13

我正在尝试通过 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)
4

1 回答 1

16

我让它运行起来,这对我来说就像app.pyAWS Chalice 项目中一样:

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 自动生成的。将AWS IAM Web 界面中现有策略旁边的策略(例如AmazonS3FullAccess)手动附加到您的 Chalice 项目创建的角色。

需要提及的事情:

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