boto3如何使用元数据创建对象? [英] boto3 how to create object with metadata?

查看:120
本文介绍了boto3如何使用元数据创建对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In the example below I want to set a timestamp metadata attribute when created an S3 object. How do I do that? The documentation is not clear.

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))

推荐答案

对于boto3,您可以在boto3网站

As for boto3, You have the upload_file possibility detailed in the boto3 website here .

import boto3
#Create the S3 client
s3ressource = client(
    service_name='s3', 
    endpoint_url= param_3,
    aws_access_key_id= param_1,
    aws_secret_access_key=param_2,
    use_ssl=True,
    )

上传文件时,您必须指定密钥(基本上是您的robject/文件名),并且在创建密钥时添加元数据将使用"ExtraArgs"选项完成:

uploading a file, you have to specify the key ( which is basically your robject/file name), and adding metadata when creating the key would be done using the "ExtraArgs" option :

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

这篇关于boto3如何使用元数据创建对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆