久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何在 Python 中從 AWS 中的 lambda 函數(shù)返回二進(jìn)制

How to return binary data from lambda function in AWS in Python?(如何在 Python 中從 AWS 中的 lambda 函數(shù)返回二進(jìn)制數(shù)據(jù)?)
本文介紹了如何在 Python 中從 AWS 中的 lambda 函數(shù)返回二進(jìn)制數(shù)據(jù)?的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我無法讓 python lambda 返回二進(jìn)制數(shù)據(jù).

引用:

<塊引用>

API Gateway 將查看 Content-TypeAccept HTTP 標(biāo)頭來決定如何處理正文.

這意味著 Content-Type 響應(yīng)標(biāo)頭必須匹配 Accept 請求標(biāo)頭

解決方案:

  1. 將 API 網(wǎng)關(guān)中的二進(jìn)制媒體類型設(shè)置為您的 mime 類型:image/jpg

  2. 在你的 HTTP 請求集中 Accept: image/jpg

  3. 在你的 HTTP 響應(yīng)集中 Content-Type: image/jpg

<塊引用>

<代碼>{isBase64Encoded":真,狀態(tài)碼":200,標(biāo)題":{內(nèi)容類型":圖像/jpg"},正文":base64.b64encode(content_bytes).decode(utf-8")}

  1. 接下來,我們必須告訴 CloudFront 接受請求中的Accept"標(biāo)頭.因此,在 CloudFront 分發(fā)中,單擊您的 API Gateway 實(shí)例(ID 可單擊),一旦重定向到 CloudFront 實(shí)例,轉(zhuǎn)到 Behaviour 選項(xiàng)卡,選擇您的 API 的路徑模式(例如:/api/*) 并點(diǎn)擊編輯按鈕.

<塊引用>

在新屏幕上,您必須將 Accept 標(biāo)頭添加到白名單.

<塊引用>

注意 1:如果您有多種文件類型,則必須將它們?nèi)刻砑拥?API 網(wǎng)關(guān)設(shè)置中的 Binary Media Types

注意 2:對于那些來自 serverless 并希望在部署 lambdas 時(shí)設(shè)置二進(jìn)制類型的用戶,請查看此帖子:為 API 網(wǎng)關(guān)設(shè)置二進(jìn)制媒體類型

插件:- 無服務(wù)器-apigw-二進(jìn)制風(fēng)俗:apigw二進(jìn)制:類型:- '圖像/JPEG'

cloudfront 的 serverless.yml 文件應(yīng)包含:

資源:WebAppCloudFront 分布:類型:AWS::CloudFront::Distribution特性:分布配置:...緩存行為:...-#API 調(diào)用...轉(zhuǎn)發(fā)值:...標(biāo)題:- 授權(quán)- 接受

I cannot get python lambda to return binary data. The node-template for thumbnail images works fine but I cannot get a python lambda to work. Below is the relevant lines from my lambda. The print("image_data " + image_64_encode) line prints a base64 encoded image to the logs.

def lambda_handler(event, context):
    img_base64 = event.get('base64Image')
    if img_base64 is None:
        return respond(True, "No base64Image key")

    img = base64.decodestring(img_base64)
    name = uuid.uuid4()
    path = '/tmp/{}.png'.format(name)

    print("path " + path)

    image_result = open(path, 'wb')
    image_result.write(img)
    image_result.close()

    process_image(path)

    image_processed_path = '/tmp/{}-processed.png'.format(name)
    print("image_processed_path " + image_processed_path)
    image_processed = open(image_processed_path, 'rb')
    image_processed_data = image_processed.read()
    image_processed.close()
    image_64_encode = base64.encodestring(image_processed_data)

    print("image_data " + image_64_encode)


    return respond(False, image_64_encode)


def respond(err, res):
    return {
        'statusCode': '400' if err else '200',
        'body': res,
        'headers': {
            'Content-Type': 'image/png',
        },
        'isBase64Encoded': 'true'
    }

Any pointers to what I'm doing wrong?

解決方案

Following all the steps above didn't work on my case, because having the binary support for content-type = */* will convert all responses to binary.

My case:

  • Multiple lambda functions returning json (text), just a single lambda returning a binary file. All have lambda proxy enabled.

  • The lambdas are in an API Gateway

  • The API Gateway is behind CloudFront

Hint: I have notice an important information in the API Gateway -> Settings

Quoting:

API Gateway will look at the Content-Type and Accept HTTP headers to decide how to handle the body.

This means that the Content-Type response header must match Accept request header

Solution:

  1. Set Binary Media Types in API gateway to your mime type: image/jpg

  2. In your HTTP request set Accept: image/jpg

  3. In your HTTP response set Content-Type: image/jpg

{
  "isBase64Encoded": True,
  "statusCode": 200,
  "headers": { "content-type": "image/jpg"},
  "body":  base64.b64encode(content_bytes).decode("utf-8")
}

  1. Next we must tell CloudFront to accept the 'Accept' header from the request. So, in CloudFront distribution, click on your API Gateway instance (ID is clickable) and once redirected to CloudFront instance go to Behaviour tab, select the path-pattern of your API (example: /api/*) and click on Edit button.

On the new screen, you have to add Accept header to Whitelist.

Note 1: If you have multiple file types, you must add them all to Binary Media Types in the API gateway settings

Note 2: For those coming from serverless and want to set the binary types when deploying your lambdas, then check this post: setting binary media types for API gateway

plugins:
  - serverless-apigw-binary

custom:
  apigwBinary:
    types:
- 'image/jpeg'

The serverless.yml file for cloudfront should contain:

resources:
    WebAppCloudFrontDistribution:
      Type: AWS::CloudFront::Distribution
      Properties:
        DistributionConfig:
          ...
          CacheBehaviors:
            ...
            - 
              #API calls
              ...
              ForwardedValues:
                ...
                Headers:
                  - Authorization
                  - Accept

這篇關(guān)于如何在 Python 中從 AWS 中的 lambda 函數(shù)返回二進(jìn)制數(shù)據(jù)?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區(qū)域周圍繪制一個(gè)矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個(gè)矩形邊界框中應(yīng)用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據(jù)文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: av资源在线看 | 中文字幕在线剧情 | 久久99精品久久久久久琪琪 | 欧美黑人一级爽快片淫片高清 | 久青草影院 | 色综合一区二区 | 日韩成人久久 | 欧美精品一区二区三 | 在线观看免费av片 | 涩涩视频在线观看 | 日韩在线观看 | 九九热视频这里只有精品 | 国内精品久久久久 | 国产精品视频免费播放 | 亚洲入口| 91精品国产欧美一区二区 | 国产高清精品网站 | av网站免费观看 | 久久久网| 69福利影院 | 午夜视频大全 | 久久精品国产一区二区电影 | 日本精品国产 | 亚洲人成人一区二区在线观看 | 国产精品中文 | 成年人免费看的视频 | 在线成人www免费观看视频 | 夫妻午夜影院 | 久久精品国产免费高清 | 久久91精品久久久久久9鸭 | 亚洲国产高清在线观看 | 91av视频在线播放 | 在线观看国产wwwa级羞羞视频 | 狠狠视频| 久久精片 | 丁香六月伊人 | 天天色天天射天天干 | 欧美久久久久久 | 国产欧美精品一区 | 久久精品国产免费高清 | 久草在线 |