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

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

    I found some time to experiment with this and here's what I found.

    >>> import boto
    >>> c =boto.connect_s3()
    >>> fp = open('myfiletoupload.txt')
    >>> content_length = len(fp.read())
    >>> c.generate_url(300, 'PUT', 'test-1332789015', 'foobar', headers={'Content-Length': str(content_length)}, force_http=True)
    'http://test-1332789015.s3.amazonaws.com/foobar?Signature=oUARG45mR95utXsiQYRJNiCI4x4%3D&Expires=1333731456&AWSAccessKeyId=AKIAJOTCCJRP4C3NSMYA&Content-Length=16'
    

    I was then able to use curl to PUT the file to that url like this:

    $ curl --request PUT --upload-file myfiletoupload.txt "http://test-1332789015.s3.amazonaws.com/foobar?Signature=oUARG45mR95utXsiQYRJNiCI4x4%3D&Expires=1333731456&AWSAccessKeyId=AKIAJOTCCJRP4C3NSMYA&Content-Length=16"
    

    This resulted in the file being uploaded to the bucket. So, it appears that it is possible. You might want to see if you can calculate the content-md5 value and include that in the headers but then you also have to figure out how to get curl to send that header, as well. Also, you should be able to make this work over HTTPS rather than HTTP but I haven't tried that.

提交回复
热议问题