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

后端 未结 5 1608
悲&欢浪女
悲&欢浪女 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:03

    Here's what it looks like in boto3(tested with version 1.2.3).

    First, create a presigned url with s3.generate_presigned_url method:

    >>> import boto3
    >>> s3 = boto3.client('s3')
    >>> s3.generate_presigned_url('put_object', Params={'Bucket':'YourBucket', 'Key':'YourKey'}, ExpiresIn=3600, HttpMethod='PUT')
    u'https://s3-ap-northeast-1.amazonaws.com/YourBucket/YourKey?AWSAccessKeyId=AKIAXXXXXXXXXXXXXXXX&Expires=1451061671&Signature=%2FtyAyCd5vrp13p%2FqLdoPkox7yTM%3D'
    

    PUT to S3 with a presigned URL

    $ curl \
      --request PUT \
      --upload-file path/to/file \
      "https://s3-ap-northeast-1.amazonaws.com/YourBucket/YourKey?AWSAccessKeyId=AKIAXXXXXXXXXXXXXXXX&Expires=1451061671&Signature=%2FtyAyCd5vrp13p%2FqLdoPkox7yTM%3D"
    

提交回复
热议问题