2017-05-24 41 views
0

私はChaliceを使用してS3から画像ファイルを返すシンプルなアプリケーションを構築しています。AWS Chalice S3から画像ファイルを返す

ファイルが64ビットエンコードされた後にファイルを返すことができます。しかし、私はバイナリファイルをどのように返すことができるのだろうと思っているので、ユーザーはファイルをダウンロードすることができますか?私の次のコードは動作していません。

@app.route('/binary_object/{bucket}/{key}', methods=['GET', 'PUT']) 
def binary_object(bucket, key): 
    request = app.current_request 
    if request.method == 'GET': 
     try: 
      file_path = '/tmp/{}_{}'.format(uuid.uuid4(), key) 
      s3_client.download_file(bucket, key, file_path) 
      file_size = os.path.getsize(file_path) 
      headers = {'Content-Disposition': 'attachment; filename=\"' + key + '\"', 
         'Content-Type': 'application/octet-stream', 
         # 'Content-Type': 'image/*', 
         'Content-Length': str(file_size)} 

      fsk = open(file_path, 'rb') 
      return Response(body=fsk, headers=headers, status_code=200) 
     except Exception as e: 
      print e 
      raise e 

答えて

0

ContentHandling attribute of the IntegrationResponseCONVERT_TO_BINARYに設定されていることを確認します。 AWSコンソールで、バイナリメソッドのIntegrationResponseページに移動し、「必要に応じてバイナリに変換する」を選択します。

Integration Response ContentHandling dropdown CLIに慣れている場合は、次のようなコマンドを使用することもできます。

aws apigateway update-integration-response --rest-api-id foobar 
    --resource-id barfoo --http-method GET --status-code 200 
    --patch-operations op='replace',path='/contentHandling',value='CONVERT_TO_BINARY' 
関連する問題