简体   繁体   English

使用Python请求上传到S3

[英]Uploading to S3 using Python Requests

I'd like to upload xml's directly to S3 without the use of modules like boto, boto3, or tinys3. 我想不使用boto,boto3或tinys3之类的模块直接将xml上传到S3。

So far I have written: 到目前为止,我已经写了:

 url = "https://my-test-s3.s3.amazonaws.com"
 with open(xml_file,'rb') as data:
     requests.put(url, data=data)

and I've gone and head and set the AllowedOrigin on my S3 bucket to accept my server's address. 然后我走了,在我的S3存储桶上设置了AllowedOrigin以接受服务器的地址。

This does not error when running, however, it also does not seem to be uploading anything. 运行时这不会出错,但是似乎也没有上传任何东西。

Any help would be appreciated --- I'd like to (a) get the thing to upload and (b) figure out how to apply AWSAccessKey and AWSSecretAccessKey to the request 任何帮助将不胜感激---我想(a)上传东西,(b)弄清楚如何将AWSAccessKey和AWSSecretAccessKey应用于请求

If you want to upload xml's directly to S3 without the use of modules like boto, boto3, or tinys3 I would recommend to use awscli : 如果您想不使用诸如boto,boto3或tinys3之类的模块直接将xml上传到S3,我建议使用awscli

pip install awscli
aws configure  # enter your AWSAccessKey and AWSSecretAccessKey credentials

AWSAccessKey and AWSSecretAccessKey will be stored inside ~/.aws folder permanently after using aws configure . 使用aws configure之后, AWSAccessKeyAWSSecretAccessKey将永久存储在~/.aws文件夹中。

And then you can upload files using python: 然后您可以使用python上传文件:

os.system("aws s3 cp {0} s3://your_bucket_name/{1}".format(file_path, file_name))

Docs are here . 文件在这里

you need to install awscli following this documentation . 您需要按照本文档安装awscli Then in a commandline shell, execute aws configure and follow the instruction. 然后在命令行外壳中,执行aws configure并按照说明进行操作。

to upload file, its much easier using boto 上传文件,使用boto轻松得多

import boto3
s3 = boto3.resource('s3')
s3.meta.client.upload_file(xml_file, 'yourbucket', 'yours3filepath')

Alternatively, you can use aws s3 cp command combined with python subprocess . 另外,您可以结合使用aws s3 cp命令和python subprocess

subprocess.call(["aws", "s3", "cp", xml_file, "yours3destination"])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM