boto3 - 存储桶之间的AWS lambda -copy文件

时间:2017-09-26 19:08:53

标签: python-3.x amazon-web-services amazon-s3 boto3

我正在尝试使用AWS lambda将源存储桶中的多个文件复制到目标存储桶,并收到以下错误。铲斗结构如下

Source Buckets

mysrcbucket /输入/每日/ acctno_pin_xref / ABC_ACCTNO_PIN_XREF_FULL_20170926_0.csv.gz     mysrcbucket /输入/每天/ acctno_pin_xref / ABC_ACCTNO_PIN_XREF_FULL_20170926_1.csv.gz     mysrcbucket /输入/每日/ acctno_pin_xref / ABC_ACCTNO_PIN_XREF_count_20170926.inf

目标存储桶

mydestbucket /输入/每日/ acctno_pin_xref / ABC_ACCTNO_PIN_XREF_FULL_20170926_0.csv.gz

mydestbucket /输入/每日/ acctno_pin_xref / ABC_ACCTNO_PIN_XREF_FULL_20170926_1.csv.gz     mydestbucket /输入/每日/ acctno_pin_xref / ABC_ACCTNO_PIN_XREF_count_20170926.inf

我在下面写了lambda函数但是得到了下面的错误。有人可以帮我解释一下我做错了吗

{   "errorMessage": "expected string or bytes-like object",   "errorType": "TypeError",   "stackTrace": [
    [
      "/var/task/lambda_function.py",
      17,
      "lambda_handler",
      "s3.Object(dest_bucket,dest_key).copy_from(CopySource= { 'Bucket': obj.bucket_name , 'Key' : obj.key})"
    ],
    [
      "/var/runtime/boto3/resources/factory.py",
      520,
      "do_action",
      "response = action(self, *args, **kwargs)"
    ],
    [
      "/var/runtime/boto3/resources/action.py",
      83,
      "__call__",
      "response = getattr(parent.meta.client, operation_name)(**params)"
    ],
    [
      "/var/runtime/botocore/client.py",
      312,
      "_api_call",
      "return self._make_api_call(operation_name, kwargs)"
    ],
    [
      "/var/runtime/botocore/client.py",
      575,
      "_make_api_call",
      "api_params, operation_model, context=request_context)"
    ],
    [
      "/var/runtime/botocore/client.py",
      627,
      "_convert_to_request_dict",
      "params=api_params, model=operation_model, context=context)"
    ],
    [
      "/var/runtime/botocore/hooks.py",
      227,
      "emit",
      "return self._emit(event_name, kwargs)"
    ],
    [
      "/var/runtime/botocore/hooks.py",
      210,
      "_emit",
      "response = handler(**kwargs)"
    ],
    [
      "/var/runtime/botocore/handlers.py",
      208,
      "validate_bucket_name",
      "if VALID_BUCKET.search(bucket) is None:"
    ]   ] }

Lambda函数代码

import boto3
import json
s3 = boto3.resource('s3')



def lambda_handler (event, context):
 bucket = s3.Bucket('mysrcbucket')
 dest_bucket=s3.Bucket('mydestbucket')
 print(bucket)
 print(dest_bucket)

for obj in bucket.objects.filter(Prefix='Input/daily/acctno_pin_xref/ABC_ACCTNO_PIN_XREF',Delimiter='/'):

  dest_key=obj.key
  print(dest_key)
  s3.Object(dest_bucket,dest_key).copy_from(CopySource= { 'Bucket': obj.bucket_name , 'Key' : obj.key})

1 个答案:

答案 0 :(得分:4)

问题在于:

s3.Object(dest_bucket, dest_key).copy_from(CopySource= {'Bucket': obj.bucket_name, 
                                                        'Key': obj.key})

dest_bucket更改为dest_bucket.name

s3.Object(dest_bucket.name, dest_key).copy_from(CopySource= {'Bucket': obj.bucket_name,
                                                             'Key': obj.key})

dest_bucket是一种资源,name是其标识符。