简体   繁体   English

使用boto3在桶中生成最新object的预签名s3 URL

[英]Generate presigned s3 URL of latest object in the bucket using boto3

I have a s3 bucket with multiple folders.我有一个带有多个文件夹的 s3 存储桶。 How can I generate s3 presigned URL for a latest object using python boto3 in aws for each folder asked by a user?如何在aws中为用户询问的每个文件夹使用python boto3为最新的object生成s3 presigned URL?

If it's a small bucket then recursively list the bucket, with prefix as needed.如果它是一个小桶,则递归列出该桶,并根据需要使用前缀。 Sort the results by timestamp, and create the pre-signed URL for the latest.按时间戳对结果进行排序,并为最新创建预签名的 URL。

If it's a very large bucket, this will be very inefficient and you should consider other ways to store the key of the latest file.如果是一个非常大的桶,这将是非常低效的,您应该考虑其他方式来存储最新文件的密钥。 For example: trigger a Lambda function whenever an object is uploaded and write that object's key into a LATEST item in DynamoDB (or other persistent store).例如:每当上传 object 时触发 Lambda function 并将该对象的密钥写入 DynamoDB(或其他持久存储)中的最新项目。

You can do something like你可以做类似的事情

import boto3
from botocore.client import Config
import requests
bucket = 'bucket-name'
folder = '/' #you can add folder path here don't forget '/' at last

s3 = boto3.client('s3',config=Config(signature_version='s3v4'))

objs = s3.list_objects(Bucket=bucket, Prefix=folder)['Contents']     
latest = max(objs, key=lambda x: x['LastModified'])
print(latest)

print (" Generating pre-signed url...")
url = s3.generate_presigned_url(
    ClientMethod='get_object',
    Params={
        'Bucket': bucket,
        'Key': latest['Key']
    }
)
print(url)
response = requests.get(url)
print(response.url)

here it will give the latest last modified file from the whole bucket however you can update login and update prefix value as per need.在这里它将提供整个存储桶中最新的最后修改文件,但是您可以根据需要更新登录名和更新前缀值。

if you are using Kubernetes POD, VM, or anything you can pass environment variables or use the python dict to store the latest key if required.如果您使用 Kubernetes POD、VM 或任何您可以传递环境变量或使用 python 字典来存储最新密钥(如果需要)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何创建预签名 URL 以使用 Boto3 从 S3 存储桶下载文件? - How do I create a Presigned URL to download a file from an S3 Bucket using Boto3? 如何使用带有预签名 url 的 boto3 将文件上传到 s3 - How to upload a file to s3 using boto3 with a presigned url 使用 Boto3 为带有自定义日志信息的 get_object 创建一个预先签名的 S3 URL? - Create a presigned S3 URL for get_object with custom logging information using Boto3? 如何使用 Boto3 下载 S3 存储桶的最新文件? - How to download the latest file of an S3 bucket using Boto3? 如何使用 Boto3 获取 S3 存储桶的最新文件? - How to get the latest file of an S3 bucket using Boto3? 如何获取 S3 存储桶本身的可下载 url 而不是 object url 使用 Z23EEEB4347BDD26BDDZ,botoEE? - How to get a downloadable url of S3 bucket itself not an object url using python, boto3? 如何使用boto3从url访问S3存储桶? - How to access S3 bucket from url using boto3? 如何使用 boto3 资源而不是客户端生成 S3 预签名 URL - How to generate S3 presigned URL with boto3 resource instead of client boto3 s3 generate_presigned_url ExpiresIn 无法按预期工作 - boto3 s3 generate_presigned_url ExpiresIn doesn't work as expected 如何使用 boto3 从 S3 上的预签名网址访问数据 - How to access data from presigned url(s) on S3 using boto3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM