[英]Uploading to S3 using Python Requests
我想不使用boto,boto3或tinys3之類的模塊直接將xml上傳到S3。
到目前為止,我已經寫了:
url = "https://my-test-s3.s3.amazonaws.com"
with open(xml_file,'rb') as data:
requests.put(url, data=data)
然后我走了,在我的S3存儲桶上設置了AllowedOrigin以接受服務器的地址。
運行時這不會出錯,但是似乎也沒有上傳任何東西。
任何幫助將不勝感激---我想(a)上傳東西,(b)弄清楚如何將AWSAccessKey和AWSSecretAccessKey應用於請求
如果您想不使用諸如boto,boto3或tinys3之類的模塊直接將xml上傳到S3,我建議使用awscli
:
pip install awscli
aws configure # enter your AWSAccessKey and AWSSecretAccessKey credentials
使用aws configure
之后, AWSAccessKey
和AWSSecretAccessKey
將永久存儲在~/.aws
文件夾中。
然后您可以使用python上傳文件:
os.system("aws s3 cp {0} s3://your_bucket_name/{1}".format(file_path, file_name))
文件在這里 。
您需要按照本文檔安裝awscli
。 然后在命令行外殼中,執行aws configure
並按照說明進行操作。
上傳文件,使用boto
輕松得多
import boto3
s3 = boto3.resource('s3')
s3.meta.client.upload_file(xml_file, 'yourbucket', 'yours3filepath')
另外,您可以結合使用aws s3 cp
命令和python subprocess
。
subprocess.call(["aws", "s3", "cp", xml_file, "yours3destination"])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.