国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Confused while sending binary image (jpg) data to AWS Lambda backend via AWS API Gateway
P粉300541798
P粉300541798 2024-04-05 13:09:55
0
1
832

Apologies in advance if I missed something simple! I have successfully sent images from React to AWS Lambda via AWS API Gateway. I uploaded it to an s3 bucket in the same Lambda function. The following is the React code:

// 在APP中初始化狀態(tài)
const [selectedImage, setSelectedImage] = useState();

// 輸入
<input
  accept="image/jpg, image/jpeg, image/png"
  type="file"
  onChange={imageChange}
/>
...
...
// 當(dāng)文件字段更改時(shí)觸發(fā)此函數(shù)
const imageChange = (e) => {
    if (e.target.files && e.target.files.length > 0) {
      setSelectedImage(e.target.files[0]);
    }
};
...
...
// 當(dāng)用戶點(diǎn)擊接受按鈕時(shí)觸發(fā)此函數(shù)
async function submitToBackend() {

    // 這里的圖像是jpg格式
    setSelectedImage(e.target.files[0]);
   
    const backendResponse = await fetch(awsapigatewayurl, {
      method: "PUT",
      headers: {
        "Content-Type": "image/*",
      },
      body: selectedImage,
    })
    console.log(backendResponse)
}

On the API Gateway, I enabled Lambda proxy forwarding and enabled the binary image format as shown below

Lambda proxy settings

API Gateway Binary Settings

Now here comes the weird part...at least for me.. In my Python backend code I have:

def lambda_handler(event, context):
    key = 'test.jpg'    
    data = event['body']
    decoded_data = base64.b64decode(data)
    image_filelike = io.BytesIO(decoded_data)

    # 創(chuàng)建日期文件夾,如果已經(jīng)創(chuàng)建則忽略
    current_date = str(datetime.date.today())
    s3_client.put_object(Bucket=BUCKET, Key=(current_date + '/'))

    # 將圖像上傳到S3
    s3_client.upload_fileobj(Bucket=BUCKET, 
                            Key='%s/%s' % (current_date, key), 
                            Fileobj=image_filelike)

But you see I have to do this:

decoded_data = base64.b64decode(data)

I thought I was crazy, so I went into Postman, copied the curl command, and modified it to send binary data from my command prompt like this:

curl --location --request PUT "apigatewayurl" \
--header "Content-Type: image/jpeg" \
--data-binary "@/Users/user/Documents/IMG_7138.jpg"

It works fine.

I should base64 encode the data but from what I see API Gateway or something in the chain already does that. Please tell me if I'm doing something wrong and if it should be done in a better way.

P粉300541798
P粉300541798

reply all(1)
P粉799885311

Okay, take a look at this articlehttps://aws.amazon.com/blogs/compute/handling-binary-data-using-amazon-api-gateway-http-apis/, It looks like the encoding is determined based on the "content-type" header. You can view the Python backend code by using the following code in a Lambda function and viewing the results in the CloudWatch logs:

print("isBase64Encoded: %s" % event['isBase64Encoded'])

I'll keep the code and solution here so others can use it if they need help.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template