How to generate a temporary url to upload file to Amazon S3 with boto library?

后端 未结 5 1549
悲&欢浪女
悲&欢浪女 2020-12-04 11:46

I knew how to download file in this way - key.generate_url(3600).

But when I tried to upload : key.generate_url(3600, method=\'PUT\'), the url didn\'t work. I was to

5条回答
  •  有刺的猬
    2020-12-04 12:18

    If you are using boto (not boto3), the only way I was able to get an upload to work was using generate_url_sigv4. Using vanilla generate_url caused the same error as reported in the original question. It is possible there is an AWS account setting I don't know about that controls which function works.

    In a Python interpreter with boto 2.49.0 and requests 2.22.0:

    import boto
    import os
    import requests
    os.environ['S3_USE_SIGV4'] = 'True'
    c = boto.connect_s3(host='s3.amazonaws.com')
    url = c.generate_url_sigv4(3600, 'PUT', 'my-bucket-name', 'bucket-path/to/file.txt')
    with open('file.txt') as f:
        resp = requests.put(url, data=f.read())
    
    >>> resp
    
    

    If you don't connect with a hostname, you will receive this error when generating the URL:

    boto.s3.connection.HostRequiredError: BotoClientError: When using SigV4, you must specify a 'host' parameter.
    

    Related:
    What does ''HmacAuthV1Handler' object has no attribute 'presign'' mean?

    If you are using boto3, a presigned POST seems to be better-documented:
    https://boto3.amazonaws.com/v1/documentation/api/latest/guide/s3-presigned-urls.html#generating-a-presigned-url-to-upload-a-file

提交回复
热议问题