5

I am trying to get a .jpg file from a bucket and send it back to api gateway. I believe I have the setup correct as I see stuff being logged. It grabs the file from s3 fine, and gm is the graphicsmagick library. Not sure if I am using it right though.

In the lambda function I do this (alot of the code comes from the aws example):

async.waterfall([
    function download(next) {
        console.log(srcKey);
        console.log(srcBucket);
        // Download the image from S3 into a buffer.
        s3.getObject({
                Bucket: srcBucket,
                Key: srcKey
            },
            next);
        },
    function transform(response, next) {
        console.log(response);
        next(null, 'image/jpeg', gm(response.Body).quality(85));

    },

    function sendData(contentType, data, next){
        console.log(contentType);
        console.log(data);
        imageBuffer = data.sourceBuffer;
        context.succeed(imageBuffer);
    }
    ]
);

The response header has content-length: 85948, which doesn't seem right because the original file is only 36kb. Anyone know what I'm doing wrong?

4

2 に答える 2

4

Get Image <-> API Gateway <-> Lambda <-> S3 統合を簡単に実現できます。

ラムダでは、json の代わりに、画像 ( buffer.toString('base64')) の base64 文字列表現を返し、API Gateway に文字列をバイナリに変換させ、特定のものを追加さContent-Typeせます (そのため、特定の Accept ヘッダーを送信するように強制する限定的なバイナリ サポートを使用する必要はありません)。 )。

AWS コンソールで API Gateway に移動し、関連するメソッドに移動して設定を更新します。

  • 統合リクエスト

    • チェックを外す: Lambda プロキシ統合を使用する
  • メソッドレスポンス

    • 応答を追加 -> HTTP ステータス: 200
    • ヘッダーを追加: Content-Type
  • 統合応答 -> ヘッダー マッピング -> 応答ヘッダー -> Content-Type

    • マッピング値:'image/jpeg'(シングルクォーテーション)

コマンドラインから以下のコマンドを実行して、文字列を強制的にバイナリに変換します。まず、 API Gateway からrest-api-idresource-idを取得します。次に、CLI で実行します (rest-api-id と resource-id を独自のものに置き換えます)。

aws apigateway put-integration-response --rest-api-id <rest-api-id> --resource-id <resource-id> --http-method GET --status-code 200 --content-handling CONVERT_TO_BINARY
于 2017-01-10T12:16:41.100 に答える
2

API Gatewayで使用する場合、バイナリ コンテンツを返すために使用しないでくださいLambdaAPI GatewayデータでLambda応答するように設定されていXML/JSONます。その理由と方法について詳しくは、こちらをご覧ください。

コールバック チェーンを変更して、変更した画像を にアップロードし直してS3ください。アップロードが成功したら、ターゲット オブジェクトを送り返しURI、クライアントをそれにリダイレクトします。

于 2015-08-25T21:52:21.103 に答える