本文介绍了如何使用 boto 库生成临时 url 以将文件上传到 Amazon S3?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何通过这种方式下载文件:key.generate_url(3600)

但是当我尝试上传:key.generate_url(3600, method='PUT') 时,该 url 不起作用.有人告诉我:我们计算的请求签名与您提供的签名不匹配.检查您的密钥和签名方法.

我在 boto 主页上找不到关于如何使用 generate_url(method='PUT') 函数的示例代码.这里有人知道如何使用它来上传吗?如何设置上传文件路径的参数?

解决方案

我找了一些时间来试验这个,这就是我的发现.

>>>进口博托>>>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&16;

然后我就可以使用 curl 将文件放入该 url 中,如下所示:

$ curl --request PUT --upload-file myfiletoupload.txt "http://test-1332789015.s3.amazonaws.com/foobar?Signature=oUARG45mR95utXsiQYRJNiCI4x4%3D&Expires=13337314456&Expires=13337314456&;内容长度=16"

这导致文件被上传到存储桶.所以,看起来这是可能的.您可能想看看是否可以计算 content-md5 值并将其包含在标头中,但您还必须弄清楚如何让 curl 发送该标头.此外,您应该能够通过 HTTPS 而不是 HTTP 进行这项工作,但我还没有尝试过.

I know how to download a 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 told:The request signature we calculated does not match the signature you provided. Check your key and signing method.

I cannot find example code on the boto homepage for how to use the function generate_url(method='PUT'). Does anyone here know how to use it for 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'

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.

这篇关于如何使用 boto 库生成临时 url 以将文件上传到 Amazon S3?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 03:14