我正在尝试通过Chalice将文件上传到我的S3存储桶中(我现在正在玩它,对此仍然很陌生)。但是,我似乎无法正确地做到这一点。
我正确设置了AWS,完成本教程后会成功返回一些消息。然后,我尝试进行一些上载/下载,然后出现问题。
s3 = boto3.resource('s3', region_name=<some region name, in this case oregon>)
BUCKET= 'mybucket'
UPLOAD_FOLDER = os.path.abspath('') # the file I wanna upload is in the same folder as my app.py, so I simply get the current folder name
@app.route('/upload/{file_name}', methods=['PUT'])
def upload_to_s3(file_name):
s3.meta.client.upload_file(UPLOAD_FOLDER+file_name, BUCKET, file_name)
return Response(message='upload successful',
status_code=200,
headers={'Content-Type': 'text/plain'}
)
当然,请不要担心我如何设置文件路径,除非那是问题所在。
我得到了错误日志:
在这种情况下,
file_name
只是mypic.jpg
。我想知道为什么没有提取
UPLOAD_FOLDER
部分。另外,作为引用,使用Chalice似乎使用绝对路径会很麻烦(在测试过程中,我已经看到代码已移至/var/task/
)有人知道如何正确设置吗?
编辑:
完整的脚本
from chalice import Chalice, Response
import boto3
app = Chalice(app_name='helloworld') # I'm just modifying the script I used for the tutorial
s3 = boto3.client('s3', region_name='us-west-2')
BUCKET = 'chalicetest1'
@app.route('/')
def index():
return {'status_code': 200,
'message': 'welcome to test API'}
@app.route('/upload/{file_name}, methods=['PUT'], content_types=['application/octet-stream'])
def upload_to_s3(file_name):
try:
body = app.current_request.raw_body
temp_file = '/tmp/' + file_name
with open(temp_file, 'wb') as f:
f.write(body)
s3.upload_file(temp_file, BUCKET, file_name)
return Response(message='upload successful',
headers=['Content-Type': 'text/plain'],
status_code=200)
except Exception, e:
app.log.error('error occurred during upload %s' % e)
return Response(message='upload failed',
headers=['Content-Type': 'text/plain'],
status_code=400)
最佳答案
我让它运行,这对我来说是AWS Chalice project中的app.py
:
from chalice import Chalice, Response
import boto3
app = Chalice(app_name='helloworld')
BUCKET = 'mybucket' # bucket name
s3_client = boto3.client('s3')
@app.route('/upload/{file_name}', methods=['PUT'],
content_types=['application/octet-stream'])
def upload_to_s3(file_name):
# get raw body of PUT request
body = app.current_request.raw_body
# write body to tmp file
tmp_file_name = '/tmp/' + file_name
with open(tmp_file_name, 'wb') as tmp_file:
tmp_file.write(body)
# upload tmp file to s3 bucket
s3_client.upload_file(tmp_file_name, BUCKET, file_name)
return Response(body='upload successful: {}'.format(file_name),
status_code=200,
headers={'Content-Type': 'text/plain'})
您可以直接从命令行使用curl及其
--upload-file
对此进行测试:curl -X PUT https://YOUR_API_URL_HERE/upload/mypic.jpg --upload-file mypic.jpg --header "Content-Type:application/octet-stream"
为了使此运行,您必须手动附加策略以将s3 写入到您的lambda函数的角色中。此角色由Chalice自动生成。 Attach the policy (e.g.
AmazonS3FullAccess
) manually中现有策略旁边的AWS IAM web interface到您的Chalice项目创建的角色。要提的事情:
/var/task/
,但是/tmp/
上有一些空间,请参见this answer。 'application/octet-stream'
指定可接受的内容类型@app.route
(并通过curl
相应地上传文件)。