【发布时间】:2019-07-14 05:41:00
【问题描述】:
我正在尝试使用 boto3 创建一些 s3 存储桶和对象,然后添加一些标签(对象标记)。然后,我想使用 IAM 来控制使用这些标签对这些对象的访问。我找不到正确的语法。 我正在使用 create_buckets.py (创建存储桶和对象),然后使用 list_objects.py 列出它们。
如果有人可以帮助我向对象和存储桶添加多个标签的语法,我将不胜感激。
create_buckets.py
import boto3
from random import randint
import json
session = boto3.session.Session()
s3 = boto3.resource('s3')
state=["Alabama","Alaska","Arizona","Arkansas","California","Colorado","Connecticut"]
for x in state:
bucket_name = x.lower() + '-season-' + str(randint(0, 10000))
s3.create_bucket(Bucket=bucket_name)
for bucket in s3.buckets.all():
print(bucket.name)
s3.Object(bucket.name,'hello.txt').put(Body=open('/tmp/hello.txt','rb'))
copy_source={ 'Bucket':bucket.name,'Key':'hello123.txt'}
list_objects.py
import boto3
s3 = boto3.resource('s3')
for bucket in s3.buckets.all():
print(bucket.name)
for obj in bucket.objects.all():
print(' ' + obj.key)
【问题讨论】:
-
这是回答你的问题吗? stackoverflow.com/questions/15827196/…
-
我不认为。文档说 tag_set (list) -- (dict) -- Key (string) -- 标签的名称。 Value (string) -- 标签的值。
标签: python amazon-web-services amazon-s3