使用预签名方式将文件上传到“冰川即时检索”并附上标签。

huangapple go评论48阅读模式
英文:

Using presigned to upload a file to "Glacier Instant Retrieval" with tags

问题

I'm using the following code:

import boto3
import requests

s3_client = boto3.client(
    "s3",
    region_name=AWS_REGION,
    aws_access_key_id=AWS_ACCESS_KEY_ID,
    aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
)

url = s3_client.generate_presigned_url(
    "put_object",
    Params=dict(
        Bucket=MY_BUCKET,
        Key=key,
        StorageClass="GLACIER_IR",
        Tagging="key1=value1&key2=value2"
    ),
    ExpiresIn=EXPIRATION,
)

with open(file_path, "rb") as file:
    data = file.read()
    response = requests.put(url, data=data)

The response is 200, the file is uploaded, but the tags are missing. I tried to do the same with the regular boto3 function (see code below) and it did work, so why the presigned URL does not?

s3_client.put_object(Bucket=MY_BUCKET, Key=key, Body=file, StorageClass="GLACIER_IR",
                     Tagging="key1=value1&key2=value2")

I also tried to change the storage class and got the same problem.

英文:

I'm using the following code:

import boto3
import requests

s3_client = boto3.client(
    "s3",
    region_name=AWS_REGION,
    aws_access_key_id=AWS_ACCESS_KEY_ID,
    aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
)

url = s3_client.generate_presigned_url(
    "put_object",
    Params=dict(
        Bucket=MY_BUCKET,
        Key=key,
        StorageClass="GLACIER_IR",
        Tagging="key1=value1&key2=value2"
    ),
    ExpiresIn=EXPIRATION,
)

with open(file_path, "rb") as file:
    data = file.read()
    response = requests.put(url, data=data)

The response is 200, the file is uploaded, but the tags are missing. I tried to do the same with the regular boto3 function (see code below) and it did work, so why the presigned url does not?

s3_client.put_object(Bucket=MY_BUCKET, Key=key, Body=file, StorageClass="GLACIER_IR",
                         Tagging="key1=value1&key2=value2")

I also tried to change the storage class and got the same problem

答案1

得分: 0

"As luk3202 said, the tags are specified during the upload, so I tried:

response = requests.put(url, data=data,
headers={"x-amz-tagging": "key1=value1&key2=value2"})

and it worked!"

英文:

As luk3202 said, the tags are specified during the upload, so I tried:

response = requests.put(url, data=data,
                            headers={"x-amz-tagging": "key1=value1&key2=value2"})

and it worked!

huangapple
  • 本文由 发表于 2023年5月21日 21:40:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76300198.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定