通过Chalice API调用将文件上传到AWS S3 [英] Uploading file to AWS S3 through Chalice API call

查看:106
本文介绍了通过Chalice API调用将文件上传到AWS S3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过Chalice将文件上传到我的S3存储桶中(我目前正在使用它,这仍然是新事物).但是,我似乎无法正确解决问题.

I'm trying to upload a file to my S3 bucket through Chalice (I'm playing around with it currently, still new to this). However, I can't seem to get it right.

我正确设置了AWS,成功完成本教程后会向我返回一些消息.然后,我尝试进行一些上载/下载,然后出现问题.

I have AWS setup correctly, doing the tutorial successfully returns me some messages. Then I try to do some upload/download, and problem shows up.

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'}
    )

请不要担心我如何设置文件路径,除非那是问题所在.

Please don't worry about how I set my file path, unless that's the issue, of course.

我收到了错误日志:

没有这样的文件或目录:

No such file or directory: ''

在这种情况下,file_name就是mypic.jpg.

我想知道为什么UPLOAD_FOLDER部分没有被拾取.另外,作为参考,使用Chalice似乎使用绝对路径会很麻烦(在测试过程中,我已经看到代码已移至/var/task/)

I'm wondering why the UPLOAD_FOLDER part is not being picked up. Also, for the reference, it seems like using absolute path will be troublesome with Chalice (while testing, I've seen the code being moved to /var/task/)

有人知道如何正确设置吗?

Does anyone know how to set it up correctly?

完整的脚本

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)

推荐答案

我运行了它,并且在app.py的身份对我有用="noreferrer"> AWS Chalice项目:

I got it running and this works for me as app.py in an AWS Chalice project:

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 及其

You can test this with curl and its --upload-file directly from the command line with:

curl -X PUT https://YOUR_API_URL_HERE/upload/mypic.jpg --upload-file mypic.jpg --header "Content-Type:application/octet-stream"

要使其运行,您必须手动附加策略以将s3写入s3 到您的lambda函数的角色.此角色由Chalice自动生成.在手动将策略(例如AmazonS3FullAccess)附加到中现有策略旁边将您的Chalice项目创建的角色href ="https://console.aws.amazon.com/iam/home#/roles" rel ="noreferrer"> AWS IAM Web界面.

To get this running, you have to manually attach the policy to write to s3 to the role of your lambda function. This role is auto-generated by Chalice. Attach the policy (e.g. AmazonS3FullAccess) manually next to the existing policy in the AWS IAM web interface to the role created by your Chalice project.

要提的东西:

  • 您无法写入Lambda函数的工作目录/var/task/,但是在/tmp/处有一些空间,请参见此答案.
  • 您必须为@app.route指定接受的内容类型'application/octet-stream'(并相应地通过curl上传文件).
  • HTTP PUT将文件或资源放在特定的URI中,因此要使用PUT,必须通过HTTP将其上传到API.
  • You cannot write to the working directory /var/task/ of the Lambda functions, but you have some space at /tmp/, see this answer.
  • You have to specify the accepted content-type 'application/octet-stream' for the @app.route (and upload the file accordingly via curl).
  • HTTP PUT puts a file or resource at a specific URI, so to use PUT this file has to be uploaded to the API via HTTP.

这篇关于通过Chalice API调用将文件上传到AWS S3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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