Using Amazon s3 boto library, how can I get the URL of a saved key?

前端 未结 3 950
执笔经年
执笔经年 2021-01-30 10:19

I am saving a key to a bucket with:

    key = bucket.new_key(fileName)
    key.set_contents_from_string(base64.b64decode(data))
    key.set_metadata(\'Content-Ty         


        
3条回答
  •  礼貌的吻别
    2021-01-30 11:15

    If the key is publicly readable (as shown above) you can use Key.generate_url:

    url = key.generate_url(expires_in=0, query_auth=False)
    

    If the key is private and you want to generate an expiring URL to share the content with someone who does not have direct access you could do:

    url = key.generate_url(expires_in=300)
    

    where expires is the number of seconds before the URL expires. These will produce HTTPS url's. If you prefer an HTTP url, use this:

    url = key.generate_url(expires_in=0, query_auth=False, force_http=True)
    

提交回复
热议问题