Boto3 + Python + AWS S3:如何从存储桶中的文件夹中获取最新文件

时间:2019-04-19 10:42:13

标签: python-2.7 amazon-web-services amazon-s3 aws-lambda boto3

我尝试通过API网关使用Lambda函数从S3存储桶中的文件夹中获取文件。我要执行此操作以在PowerBI / Tableau中导入csv文件的最新版本以进行数据分析。如果插入文件名,则可以执行此操作。但是,这显然不能在最新文件中解决。 我希望代码始终采用该文件夹中的最新文件。我打算通过Last Modified属性而不是通过文件名本身来完成此操作。

我的水桶看起来像这样 桶 输入   -输入file.csv 输出   -输出18042019.csv   -Output19042019.csv

但是,我所拥有的代码不允许在文件夹内搜索(据我所知),而且似乎也没有获取任何最新文件。我试过将文件放在存储桶的根文件夹中,以查看其是否有效,但无效。我该如何解决这个问题?

import json
import boto3
from datetime import datetime


def lambda_handler(event, context):
    # TODO implement
    get_last_modified = lambda obj: int(obj['LastModified'].strftime('%s'))

    client = boto3.client('s3')
    objs= client.list_objects_v2(Bucket='BUCKET')['Contents']
    last_added = [obj['Key'] for obj in sorted(objs, key=get_last_modified)][0]

    bucket='BUCKET'
    link = client.generate_presigned_url('get_object', {'Bucket': bucket, 'Key': last_added}, 7200, 'GET')
    return {
        "statusCode": 303,
        "headers": {'Location': link}
    }

我得到的错误如下:

Response:
{
  "stackTrace": [
    [
      "/var/task/lambda_function.py",
      11,
      "lambda_handler",
      "objs= client.list_objects_v2(Bucket='BUCKET')['Contents']"
    ],
    [
      "/var/runtime/botocore/client.py",
      314,
      "_api_call",
      "return self._make_api_call(operation_name, kwargs)"
    ],
    [
      "/var/runtime/botocore/client.py",
      612,
      "_make_api_call",
      "raise error_class(parsed_response, operation_name)"
    ]
  ],
  "errorType": "ClientError",
  "errorMessage": "An error occurred (AllAccessDisabled) when calling the ListObjectsV2 operation: All access to this object has been disabled"
}

Request ID:
"afd650be-4841-43cf-9cf4-731390bea1ce"

Function Logs:
START RequestId: afd650be-4841-43cf-9cf4-731390bea1ce Version: $LATEST
An error occurred (AllAccessDisabled) when calling the ListObjectsV2 operation: All access to this object has been disabled: ClientError
Traceback (most recent call last):
  File "/var/task/lambda_function.py", line 11, in lambda_handler
    objs= client.list_objects_v2(Bucket='BUCKET')['Contents']
  File "/var/runtime/botocore/client.py", line 314, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/var/runtime/botocore/client.py", line 612, in _make_api_call
    raise error_class(parsed_response, operation_name)
ClientError: An error occurred (AllAccessDisabled) when calling the ListObjectsV2 operation: All access to this object has been disabled

END RequestId: afd650be-4841-43cf-9cf4-731390bea1ce
REPORT RequestId: afd650be-4841-43cf-9cf4-731390bea1ce  Duration: 2125.21 ms    Billed Duration: 2200 ms    Memory Size: 128 MB Max Memory Used: 58 MB  

0 个答案:

没有答案