通过Chalice API调用将文件上载到AWS S3

时间:2017-11-12 10:56:54

标签: python python-3.x boto3 chalice

我试图通过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'}
    )

请不要担心我如何设置文件路径,当然,除非这是问题所在。

我收到了错误日志:

  

没有这样的文件或目录:&#39;&#39;

在这种情况下,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)

1 个答案:

答案 0 :(得分:13)

我让它正常运行,这对AWS Chalice projectapp.py的{​​{1}}起作用了:

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项目创建的角色。

值得一提的是:

  • 您无法写入Lambda函数的工作目录/var/task/,但/tmp/有一些空格,请参阅this answer
  • 您必须为'application/octet-stream'指定已接受的内容类型@app.route(并通过curl相应地上传文件)。
  • HTTP PUT将文件或资源放在特定的URI上,因此要使用PUT,必须通过HTTP将此文件上传到API。