BOTO:如何使用S3作为资源或客户端,但不是两者兼而有之?

发布于 2025-02-10 04:10:06 字数 836 浏览 1 评论 0原文

这是Boto和AWS的完整NEWB简介。目前,我唯一的目标是能够访问外部代理机构的S3存储桶,因此我想了解如何特别使用它们。这个最小的代码可以完成我想做的事情,但是我还没有通过仅将S3R作为资源并避免使用S3作为客户端来弄清楚如何使用它。似乎最好是从S3资源访问存储桶,然后专门与存储桶,即,bucket.new_key('testdir/')bucket.put_object(key) =('testdir/))。这是可能的,还是有充分的理由重新构架我如何接近这一点?谢谢!

import boto3

bucket_name = 'my-bucket-name'
region_name = 'my-region-name'

print('Acquiring s3 service')
s3 = boto3.client('s3', region_name=region_name)
s3r = boto3.resource('s3', region_name=region_name)OB

print('Accessing bucket')
bucket = s3r.Bucket(bucket_name)

print('Emptying bucket')
bucket.objects.all().delete()

print('Uploading folder structures')
s3.put_object(Bucket=bucket_name, Key=('testdir/'))
s3.put_object(Bucket=bucket_name, Key=('testdir/subdir1/'))
s3.put_object(Bucket=bucket_name, Key=('testdir/subdir2/'))

This is my complete newb intro to Boto and AWS. At the moment my only goal is to be able to access an external agency's S3 bucket, so I want to understand how to use them in particular. This minimal code does what I want it to do, but I haven't figured out how to use this by only declaring s3r as a resource and avoiding having to use s3 as a client. It seems like it would be better to access the bucket from the S3 resource and then work exclusively with the bucket, i.e., bucket.new_key('testdir/') or bucket.put_object(Key=('testdir/)). Is this possible, or is there alternately a good reason to reframe how I'm approaching this? Thanks!

import boto3

bucket_name = 'my-bucket-name'
region_name = 'my-region-name'

print('Acquiring s3 service')
s3 = boto3.client('s3', region_name=region_name)
s3r = boto3.resource('s3', region_name=region_name)OB

print('Accessing bucket')
bucket = s3r.Bucket(bucket_name)

print('Emptying bucket')
bucket.objects.all().delete()

print('Uploading folder structures')
s3.put_object(Bucket=bucket_name, Key=('testdir/'))
s3.put_object(Bucket=bucket_name, Key=('testdir/subdir1/'))
s3.put_object(Bucket=bucket_name, Key=('testdir/subdir2/'))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吹梦到西洲 2025-02-17 04:10:06

BOTO3 API为大多数AWS API提供了“客户端”和“资源”对象模型。 文档请稍后再提一下):

资源代表亚马逊Web服务(AWS)的面向对象的接口。他们提供的抽象较高,而不是由服务客户端进行的原始低级呼叫

换句话说,“客户端” API是对基础AWS休息的相当一对一的包装器。 “资源” API调用旨在易于使用,它们提供了一些“生活质量”改进,从而使编写代码更快。使用哪一个在很大程度上归结为编码样式的偏好。在大多数情况下,您可以通过“资源”调用来实现“客户端”调用可以实现的目标。并非总是如此。当然,就您的例子而言,无论哪种情况:

s3 = boto3.client('s3')

# List all of the objects in a bucket, note that since we're fairly
# close to the underlying REST API with the client interface, we need
# to worry about paginating the list objects
paginator = s3.get_paginator('list_objects_v2')
for page in paginator.paginate(Bucket=bucket_name):
    for cur in page.get('Contents', []):
        # And delete each object in turn
        s3.delete_object(Bucket=bucket_name, Key=cur['Key'])

# Create a zero-byte object to represent the folder
s3.put_object(Bucket=bucket_name, Key='testdir/')

使用资源界面可以完成相同的工作,

s3r = boto3.resource('s3')

# Same idea with resource
bucket = s3r.Bucket(bucket_name)
# Paginating, and calling delete on each object in turn is handled
# behind the scenes by all() and delete() in turn
bucket.objects.all().delete()
# Creating the object, again make a zero-byte object to mimic creating
# a folder as the S3 Web UI does
bucket.put_object(Key='testdir/')

但是文档中有一个警告:但是:

AWS Python SDK团队不打算在BOTO3中的资源接口添加新功能。现有的接口将在BOTO3的生命周期期间继续运行。客户可以通过客户端界面找到对新服务功能的访问。

这取决于个人喜好。我个人更喜欢使用客户端界面,因为它可以更容易理解和跟踪哪些基础API调用以及提供新的AWS功能,但这取决于您。

The boto3 API provides both a 'client' and 'resource' object model for most of the AWS APIs. The documentation has this to say on the difference (with a caveat I'll mention later):

Resources represent an object-oriented interface to Amazon Web Services (AWS). They provide a higher-level abstraction than the raw, low-level calls made by service clients

In other words, the 'client' APIs are a fairly one to one wrapper over the underlying AWS REST calls. The 'resource' API calls are meant to be easier to use, and they provide some "quality of life" improvements that make writing code quicker. Which one to use largely comes down to a coding style preference. For the most part what you can accomplish with 'client' calls can also be accomplished with 'resource' calls. Not always, though. Certainly, for your example, it's possible in either case:

s3 = boto3.client('s3')

# List all of the objects in a bucket, note that since we're fairly
# close to the underlying REST API with the client interface, we need
# to worry about paginating the list objects
paginator = s3.get_paginator('list_objects_v2')
for page in paginator.paginate(Bucket=bucket_name):
    for cur in page.get('Contents', []):
        # And delete each object in turn
        s3.delete_object(Bucket=bucket_name, Key=cur['Key'])

# Create a zero-byte object to represent the folder
s3.put_object(Bucket=bucket_name, Key='testdir/')

The same work can be accomplished with the resource interface

s3r = boto3.resource('s3')

# Same idea with resource
bucket = s3r.Bucket(bucket_name)
# Paginating, and calling delete on each object in turn is handled
# behind the scenes by all() and delete() in turn
bucket.objects.all().delete()
# Creating the object, again make a zero-byte object to mimic creating
# a folder as the S3 Web UI does
bucket.put_object(Key='testdir/')

There is a caveat in the documentation, however:

The AWS Python SDK team does not intend to add new features to the resources interface in boto3. Existing interfaces will continue to operate during boto3’s lifecycle. Customers can find access to newer service features through the client interface.

It comes down to personal preferences. I personally prefer using the client interface, since it makes it easier to understand and track which underlying API calls are being made as well as provides new AWS functionality, but it's really up to you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文