如何生成一个临时的URL文件上传到亚马逊的S3与博托库?亚马逊、文件上传、URL、博托库

2023-09-11 23:38:20 作者:忆我清水

我知道如何下载文件以这种方式 - key.generate_url(3600)

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

但是,当我尝试上传:key.generate_url(3600,方法='把'),网址没有工作。有人告诉我:   我们计算请求签名不您提供的签名相匹配。请检查您的密钥和签名方法。

But when I tried to upload : key.generate_url(3600, method='PUT'), the url didn't work. I was told: "The request signature we calculated does not match the signature you provided. Check your key and signing method."

我无法找到例如code在博托主页,如何使用功能generate_url(方法='把')。是否有人在这里知道如何使用它的上传?如何设置PARAMS上传文件的路径?

I cannot found example code in boto homepage for how to use the function generate_url(method='PUT'). Does anyone here know how to use it for the uploading? how to set the params for the path of upload file?

推荐答案

我发现了一些时间来试验这一点,这是我发现了。

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'

我当时能够使用curl文件PUT到该​​网址是这样的:

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"

这导致在文件中被上传到桶中。所以,似乎是可能的。你可能想看看你是否可以计算内容的MD5值,其中包括,在头,但那么你也必须找出如何让卷曲发送标题,以及。此外,你应该能够使这项工作通过HTTPS,而不是HTTP,但我还没有尝试过。

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.