Bulk Generate Pre-Signed URLs boto3

百般思念 提交于 2021-02-07 19:51:00

问题


I am currently using the following to create a pre-signed url for a bucket resource:

bucket_name = ...
key = ...
s3_client = ...

s3_client.generate_presigned_url(
    ClientMethod="get_object",
    Params={
        "Bucket": bucket_name,
        "Key": key
    },
    ExpiresIn=100
)

This works fine. However, I was wondering if it was possible to generate pre-signed urls for multiple keys in one request? Or is it required to make one request for each key? I didn't find anything useful in the docs regarding this topic. I'm looking for something like this:

bucket_name = ...
keys = [...]
s3_client = ...

# Returns an array of pre-signed urls, in the same order as `keys`
s3_client.generate_presigned_url(
    ClientMethod="get_object",
    Params={
        "Bucket": bucket_name,
        "Keys": keys
    },
    ExpiresIn=100
)

回答1:


Generating presigned URLs is actually done locally, without requiring a call to AWS. This is because all necessary information (Bucket, Key, Secret Key) is known locally and can generate the signature.

Therefore, feel free to call that function repeatedly since there is no network/service overhead.

In general, there should be no need to bulk-generate URLs. Instead, whenever your application wishes to reference a resource (eg an image on an HTML page), it can quickly call the generate_presigned_url() function with an appropriate timeout.



来源:https://stackoverflow.com/questions/53951788/bulk-generate-pre-signed-urls-boto3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!