在Cloudwatch事件的结构中:
'events': [
{
'logStreamName': 'string',
'timestamp': 123,
'message': 'string',
'ingestionTime': 123,
'eventId': 'string'
},
]
Run Code Online (Sandbox Code Playgroud)
摄入时间是什么意思?
他们在这份文件中说:
ingestionTime
Run Code Online (Sandbox Code Playgroud)The time the event was ingested.
我发现仍然不清楚.
我有这个功能,可以将存档文件上传到 S3 存储桶:
def upload_file_to_s3_bucket(self, bucket, file, key, log):
if not os.path.exists(file):
log.error("File '%s' does not exist." % file)
tools.exit_gracefully(log)
log.info("Uploading file '%s' to bucket '%s' ..." % (file, bucket))
try:
self._s3.upload_file(file, bucket, key)
except botocore.exceptions.ClientError as e:
log.error("Unexpected uploading error : %s" % e)
tools.exit_gracefully(log)
log.info("Uploading finished.")
Run Code Online (Sandbox Code Playgroud)
我想对其进行单元测试,这是到目前为止我可以写的内容:
class TestUploadFilesToS3(unittest.TestCase):
""" Tests unitaires upload_file_to_s3_bucket"""
def setUp(self):
conf.LOG_FILE = "/tmp/test.log"
conf.BUCKET_OUTPUT="name.of.the.bucket"
conf.Conf.get_level_log()
self.log = logger(conf.LOG_FILE, conf.LEVEL_LOG).logger
tools.create_workdir(self.log)
conf.WORKDIR = os.path.join(conf.LOCAL_DIR, "files/output")
archive = "file_archive.tar.gz"
archivePath = "/tmp/clients/file_archive.tar.gz"
_aws = None …Run Code Online (Sandbox Code Playgroud) 我想计算 python 中 S3 存储桶中所有文件的大小,这是我迄今为止尝试过的代码:
import boto3
s3_client = boto3.client('s3')
bucket = 'bucket-name'
prefix = 'path/to/folder'
len=0
response = s3_client.list_objects(Bucket = bucket,Prefix = prefix)
for file in response['Contents']:
name = file['Key'].rsplit('/', 1)
len+=name['ContentLength']
Run Code Online (Sandbox Code Playgroud)
我不确定如何获得文件的大小:有name['ContentLength']
什么想法吗?
我在通过邮件收到的 cloudwatch 中有这行 lambda 函数日志:
/aws/lambda/sns-function | 2017/01/10/[$LATEST]425d9138c8d54ab57l0766ba74fdfd4p | 2017-01-10T00:04:30.734Z | 2017-01-10 00:04:30,734 :: ERROR :: error creating /tmp/tmpkRWp3S_20170110/file20170115.tar.gz: Command `['/bin/tar', '--create', '-z', '--file', u'/tmp/tmpkRWp3S_20170110/file20170115.tar.gz', '--', './']' returned non-zero exit status 1
Run Code Online (Sandbox Code Playgroud)
如本文档中所述,我想放置过滤器模式以仅获取重要数据。对我来说,我只想获取一次日期,因为在上面的行中,我有两次此信息:2017-01-10T00:04:30.734Z
我尝试使用这样的模式:
[...,timestamp,level,message=*ERROR*,...]
Run Code Online (Sandbox Code Playgroud)
但我收到了这个错误:
2017-01-17 10:45:58,091 :: ERROR :: logGroup: '/aws/lambda/sns-function' - logStream: 'None'
2017-01-17 10:45:58,091 :: ERROR :: An error occurred (InvalidParameterException) when calling the FilterLogEvents operation: Duplicate field '...'
Run Code Online (Sandbox Code Playgroud)
如何解析日志以获取一次日期?
我有这个 cloudformation 模板:
"InstanceProfileProd": {
"Type" : "AWS::IAM::InstanceProfile",
"Properties": {
"Path": "/",
"Roles" : [ { "Ref" : "InstanceRole"} ]
}
},
"CompLayer": {
"Type": "AWS::OpsWorks::Layer",
"DependsOn" : "OpsWorksServiceRole",
"Properties": {
"AutoAssignElasticIps" : false,
"AutoAssignPublicIps" : true,
"CustomJson" : {
},
"awscli" : {
"profils" : {
"default" : {
"role_arn": { "Fn::GetAtt": [ "InstanceProfileProd","Arn" ] }
}
}
},
},
"CustomSecurityGroupIds" : { "Ref" : "SecurityGroupIds" },
"EnableAutoHealing" : true,
"InstallUpdatesOnBoot": false,
"LifecycleEventConfiguration": {
"ShutdownEventConfiguration": {
"DelayUntilElbConnectionsDrained": false,
"ExecutionTimeout": …Run Code Online (Sandbox Code Playgroud) 我在模板中使用访问密钥创建了一个用户:
"MyAccessKey" : {
"Type" : "AWS::IAM::AccessKey",
"Properties" : {
"UserName" : { "Ref" : "User12" }
}
}
Run Code Online (Sandbox Code Playgroud)
我需要在模板的输出中获取访问密钥ID和秘密密钥。怎么做 ?谢谢
我正在尝试解析日期并将其放入正确的格式,这是我迄今为止尝试过的:
import csv
import sys
import os
import re
import fnmatch
import csv
from dateutil.parser import parse as parseDate
from datetime import datetime, time, timedelta
chaine = '16/12/201602:15:00'
date = chaine[:10] + " " + chaine[11:]
print date
newd = parseDate(date, yearfirst=True)
print newd
newd = newd.replace('-','')
newd = newd.replace(':','')
print newd
Run Code Online (Sandbox Code Playgroud)
这是我得到的结果:
16/12/2016 2:15:00
2016-12-16 02:15:00
Traceback (most recent call last):
File "t.py", line 25, in <module>
newd = newd.replace('-','')
TypeError: an integer is required
Run Code Online (Sandbox Code Playgroud)
我在这里想念什么?
谢谢
这是角色片段:
"InstanceRole": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Version" : "2012-10-17",
"Statement": [ {
"Effect": "Allow",
"Principal": {
"Service" : [ { "Fn::FindInMap": [ "Region2Principal", { "Ref": "AWS::Region" },"EC2Principal" ] } ] },
"Action" : [ "sts:AssumeRole" ]
}]
},
"Path": "/",
"Policies": [{
"PolicyName": {"Fn::Join" : ["",["AWS::StackName","InstanceApi"] ] },
"PolicyDocument": {
"Statement": [{
"Effect": "Allow",
"Action": "*",
"Resource": "*"
}]
}
}]
}
},
Run Code Online (Sandbox Code Playgroud)
这是错误: The specified value for policyName is invalid. It must contain only alphanumeric characters …
测试此代码后,我收到此错误:
'EC2' object has no attribute 'instances': AttributeError
Traceback (most recent call last):
File "/var/task/lambda_function.py", line 11, in lambda_handler
instances=ec2.instances.filter(Filters=filters)
AttributeError: 'EC2' object has no attribute 'instances'
Run Code Online (Sandbox Code Playgroud)
第11行是下面代码中的最后一行
ec2 = boto3.client('ec2', region_name=region)
def lambda_handler(event, context):
filters = [{ 'Name': 'instance-state-name', 'Values': ['running']}]
instances=ec2.instances.filter(Filters=filters)
Run Code Online (Sandbox Code Playgroud)
这里的错误究竟在哪里?
python ×4
amazon-s3 ×2
json ×2
access-keys ×1
amazon-iam ×1
aws-opsworks ×1
boto3 ×1
file ×1
lambda ×1
logging ×1
roles ×1
size ×1
string ×1
unit-testing ×1