boto3 如何使用元数据创建对象?

问题描述 投票:0回答:3

在下面的示例中,我想在创建 S3 对象时设置时间戳元数据属性。我怎么做?文档不清楚。

import uuuid
import json
import boto3
import botocore
import time

from boto3.session import Session
session = Session(aws_access_key_id='XXX',
                  aws_secret_access_key='XXX')

s3 = session.resource('s3')

bucket = s3.Bucket('blah')

for filename in glob.glob('json/*.json'):
    with open(filename, 'rb') as f:
        data = f.read().decode('utf-8')
        timestamp = str(round(time.time(),10))
        my_s3obj = s3.Object('blah', str(uuid.uuid4())).put(Body=json.dumps(data))
amazon-web-services amazon-s3 boto boto3
3个回答
18
投票

至于 boto3,您可以在 boto3 网站

这里
中详细了解 upload_file() 选项。

import boto3
s3 = boto3.client('s3')
s3.upload_file('/tmp/hello.txt', 'mybucket', 'hello.txt')

上传文件时,您必须指定密钥(基本上就是您的对象/文件名)。创建密钥时添加元数据将使用“ExtraArgs”选项完成:

s3ressource.upload_file(
    Filename, bucketname, key, 
    ExtraArgs={
        "Metadata": {
            "metadata1": "ImageName",
            "metadata2": "ImagePROPERTIES",
            "metadata3": "ImageCREATIONDATE"
        }
    }
)

9
投票

您可以将对象的元数据指定为键值对,如下所示:

s3.Object('bucket-name', 'uuid-key-name').put(Body='data', 
                                              Metadata={'key-name':'value'})

请参阅 boto3 文档,了解您可以在

put()
中使用的其他参数。


0
投票
import boto3

def convert_key_values_to_string(data):
    """
    Converts the keys and values of a dictionary to strings.

    Args:
        data (dict): The dictionary containing key-value pairs.

    Returns:
        dict: A new dictionary with keys and values as strings.
    """
    return {str(key): str(value) for key, value in data.items()}

def save_to_s3(data, metadata, bucket_name, file_key):
    """
    Saves data to an Amazon S3 bucket with specified metadata.

    Args:
        data (bytes/str): The data to be saved.
        metadata (dict): Metadata associated with the S3 object.
        bucket_name (str): The name of the S3 bucket.
        file_key (str): The key (path) under which to store the data in the bucket.

    Raises:
        Exception: If an error occurs while saving the data to S3.

    Note:
        The 'metadata' dictionary is converted to strings using the
        'convert_key_values_to_string' function as 'Metadata' in S3
        accepts dictionary items with both keys and values as strings only.
    """
    s3 = boto3.client('s3')
    try:
        s3.put_object(
            Body=data,
            Bucket=bucket_name,
            Key=file_key,
            Metadata=convert_key_values_to_string(metadata)
        )
    except Exception as e:
        print(f"Error saving {file_key}: {e}")
© www.soinside.com 2019 - 2024. All rights reserved.