gpt4 book ai didi

amazon-s3 - 使用 Cloudformation 启用 S3 库存

转载 作者:行者123 更新时间:2023-12-03 07:19:07 25 4
gpt4 key购买 nike

我可以使用控制台启用库存,如此图所示。有什么方法可以使用 boto 执行相同的操作吗?

enter image description here


更新:

这是有效的完整脚本!

import boto3

s3_client = boto3.client(
"s3",
aws_access_key_id="XXX",
aws_secret_access_key="XXX",
region_name="us-east-1",
)

response = s3_client.put_bucket_inventory_configuration(
Bucket="athenadata16",
Id="myinventory",
InventoryConfiguration={
"Destination": {
"S3BucketDestination": {
"AccountId": "1234567890",
"Bucket": "arn:aws:s3:::athenadata16",
"Format": "ORC",
"Prefix": "mypre",
}
},
"IsEnabled": True,
"Filter": {"Prefix": "myprefilter"},
"Id": "myinventory",
"IncludedObjectVersions": "Current",
"OptionalFields": [
"Size",
"LastModifiedDate",
"StorageClass",
"ETag",
"IsMultipartUploaded",
"ReplicationStatus",
"EncryptionStatus",
"ObjectLockRetainUntilDate",
"ObjectLockMode",
"ObjectLockLegalHoldStatus",
"IntelligentTieringAccessTier",
],
"Schedule": {"Frequency": "Daily"},
},
)

根据文档,可以使用 cloudformation 添加库存。

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-s3-bucket-inventoryconfiguration.html

有人可以举个例子吗?


更新2:

执行以下模板后,创建了一个名为“athenadata162a-bucketwithinventory-1snf1yx82si5c”的新存储桶。这是预料之中的。由于 BucketArn 设置, list 目标存储桶指向“athenadata162”。我需要将其指向当前存储桶名称。是否可以?

Resources:
BucketWithInventory:
Type: "AWS::S3::Bucket"
Properties:
InventoryConfigurations:
-
Destination:
BucketAccountId: !Sub '${AWS::AccountId}'
BucketArn: !Sub 'arn:aws:s3:::athenadata16'
Format: CSV
Prefix: mypre
Enabled: true
Id: myinventory
IncludedObjectVersions: Current
OptionalFields:
- Size
- LastModifiedDate
- StorageClass
- ETag
- IsMultipartUploaded
- ReplicationStatus
- ObjectLockRetainUntilDate
- ObjectLockMode
- ObjectLockLegalHoldStatus
- IntelligentTieringAccessTier
Prefix: myprefilter
ScheduleFrequency: Daily

如果我将其更改为此,我会收到循环引用错误。

BucketArn: !Sub 'arn:aws:s3:::${BucketWithInventory}'


感谢 Franklinsijo 的回答,这里是创建带有库存的存储桶的完整代码,该存储桶将在同一存储桶中保存 csv 文件。

Resources:
BucketWithInventory:
Type: "AWS::S3::Bucket"
Properties:
BucketName: !Sub 'athenadata162-${AWS::AccountId}'
InventoryConfigurations:
-
Destination:
BucketAccountId: !Sub '${AWS::AccountId}'
BucketArn: !Sub 'arn:aws:s3:::athenadata162-${AWS::AccountId}'
Format: CSV
Prefix: mypre
Enabled: true
Id: myinventory
IncludedObjectVersions: Current
OptionalFields:
- Size
- LastModifiedDate
- StorageClass
- ETag
- IsMultipartUploaded
- ReplicationStatus
- ObjectLockRetainUntilDate
- ObjectLockMode
- ObjectLockLegalHoldStatus
- IntelligentTieringAccessTier
Prefix: myprefilter
ScheduleFrequency: Daily

更新4:

当我手动添加 list 配置时,会自动添加以下存储桶策略。上面提到的 cloudformation 模板不包含此步骤,因此会出现“访问被拒绝”错误。我如何将其包含在该模板中?

{
"Id": "S3-Console-Auto-Gen-Policy-1585038423058",
"Version": "2012-10-17",
"Statement": [
{
"Sid": "S3PolicyStmt-DO-NOT-MODIFY-1585038422770",
"Effect": "Allow",
"Principal": {
"Service": "s3.amazonaws.com"
},
"Action": [
"s3:PutObject"
],
"Resource": [
"arn:aws:s3:::athenadata162-1234567890/*"
],
"Condition": {
"ArnLike": {
"aws:SourceArn": [
"arn:aws:s3:::athenadata162-1234567890"
]
},
"StringEquals": {
"aws:SourceAccount": [
"1234567890"
],
"s3:x-amz-acl": "bucket-owner-full-control"
}
}
}
]
}

更新5

模板的最终版本将如下所示......

Resources:
BucketWithInventory:
Type: "AWS::S3::Bucket"
Properties:
BucketName: !Sub '${AWS::StackName}-${AWS::AccountId}'
InventoryConfigurations:
-
Destination:
BucketAccountId: !Sub '${AWS::AccountId}'
BucketArn: !Sub 'arn:aws:s3:::${AWS::StackName}-${AWS::AccountId}'
Format: CSV
Prefix: mypre
Enabled: true
Id: myinventory
IncludedObjectVersions: Current
OptionalFields:
- Size
- LastModifiedDate
- StorageClass
- ETag
- IsMultipartUploaded
- ReplicationStatus
- ObjectLockRetainUntilDate
- ObjectLockMode
- ObjectLockLegalHoldStatus
- IntelligentTieringAccessTier
Prefix: myprefilter
ScheduleFrequency: Daily

BucketPolicyForInventoryBucket:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: !Ref BucketWithInventory
PolicyDocument:
Statement:
-
Effect: Allow
Principal:
Service: s3.amazonaws.com
Action:
- s3:PutObject
Resource:
- !Join ["", ["arn:aws:s3:::", !Ref BucketWithInventory, "/*"]]
Condition:
ArnLike:
aws:SourceArn:
- !Join ["", ["arn:aws:s3:::", !Ref BucketWithInventory, "/*"]]
StringEquals:
aws:SourceAccount:
- !Sub '${AWS::AccountId}'
s3:x-amz-acl: bucket-owner-full-control

最佳答案

Boto3 引用列出了许多使用库存配置的方法,特别是:

https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Client.put_bucket_inventory_configuration

如果您曾问自己“boto3 可以做到这一点吗?”,请转到 boto3 文档,找到相应的服务引用 ( https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/index.html ) 并在该页面上快速按 Ctrl+F。最有可能的是 boto3 确实可以做到这一点。

关于amazon-s3 - 使用 Cloudformation 启用 S3 库存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60615911/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com