如何在不创建临时本地文件的情况下将文件上传到S3 [英] How to upload a file to S3 without creating a temporary local file

查看:156
本文介绍了如何在不创建临时本地文件的情况下将文件上传到S3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有可行的方法将动态生成的文件直接上传到Amazon s3,而无需先创建本地文件然后上传到s3服务器?我使用python。谢谢

Is there any feasible way to upload a file which is generated dynamically to amazon s3 directly without first create a local file and then upload to the s3 server? I use python. Thanks

推荐答案

下面是一个示例(使用请求库)下载图像并将其上传到s3,而不写入本地文件的示例:

Here is an example downloading an image (using requests library) and uploading it to s3, without writing to a local file:

import boto
from boto.s3.key import Key
import requests

#setup the bucket
c = boto.connect_s3(your_s3_key, your_s3_key_secret)
b = c.get_bucket(bucket, validate=False)

#download the file
url = "http://en.wikipedia.org/static/images/project-logos/enwiki.png"
r = requests.get(url)
if r.status_code == 200:
    #upload the file
    k = Key(b)
    k.key = "image1.png"
    k.content_type = r.headers['content-type']
    k.set_contents_from_string(r.content)

这篇关于如何在不创建临时本地文件的情况下将文件上传到S3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆