简体   繁体   English

在AWS中标记s3

[英]Tagging s3 in AWS

I am trying to create some s3 buckets and Objects using boto3 then add some tags ( object tagging). 我正在尝试使用boto3创建一些s3存储桶和对象,然后添加一些标签(对象标签)。 I then want to use IAM to control access on these objects using these tags. 然后,我想使用IAM使用这些标签控制对这些对象的访问。 I am unable to find the right syntax. 我找不到正确的语法。 I am using create_buckets.py ( to create buckets and objects) and then list_objects.py to list them. 我正在使用create_buckets.py(创建存储桶和对象),然后使用list_objects.py列出它们。

Really appreciate if someone can help me with the syntax to add multiple tags to objects and buckets. 如果有人可以帮助我在对象和存储桶中添加多个标签的语法,我们将非常感谢。

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

You can not define tag during uploading object. 您无法在上传对象时定义标签。 Here is what I got when I tried to set tag during upload. 这是我在上传过程中尝试设置标签时得到的。

在此处输入图片说明

does not appear to be a valid way to specify a tag. 似乎不是指定标签的有效方法。

But there is way around, 但是有办法

Upload the object and then set the tag once the object is uploaded. 上载对象,然后在上载对象后设置标签。 Here is complete working example 这是完整的工作示例

import logging
import boto3
from botocore.exceptions import ClientError
client = boto3.client('s3')


def upload_file(file_name, bucket, object_name=None):
    """Upload a file to an S3 bucket

    :param file_name: File to upload
    :param bucket: Bucket to upload to
    :param object_name: S3 object name. If not specified then file_name is used
    :return: True if file was uploaded, else False
    """
    # If S3 object_name was not specified, use file_name
    if object_name is None:
        object_name = file_name

    # Upload the file
    s3_client = boto3.client('s3')
    try:
        response = s3_client.upload_file(file_name, bucket, object_name)
    except ClientError as e:
        logging.error(e)
        return False
    return True
s3 = boto3.client('s3')
with open("./test.txt", "rb") as f:

    s3.upload_fileobj(f, "config-bucket-name", "test.txt",
      ExtraArgs={
        'Metadata': {'mykey': 'myvalue'}
        })


# set tag once object upload to s3
response = client.put_object_tagging(
    Bucket='config-bucket-name',
    Key='test.txt',
    Tagging={
        'TagSet': [
            {
                'Key': 'ENV',
                'Value': 'Prod'
            },
             {
                'Key': 'type',
                'Value': 'txt'
            }
        ]
    }
)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM