BOTO:如何使用S3作为资源或客户端,但不是两者兼而有之?
这是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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
BOTO3 API为大多数AWS API提供了“客户端”和“资源”对象模型。 文档请稍后再提一下):
换句话说,“客户端” API是对基础AWS休息的相当一对一的包装器。 “资源” API调用旨在易于使用,它们提供了一些“生活质量”改进,从而使编写代码更快。使用哪一个在很大程度上归结为编码样式的偏好。在大多数情况下,您可以通过“资源”调用来实现“客户端”调用可以实现的目标。并非总是如此。当然,就您的例子而言,无论哪种情况:
使用资源界面可以完成相同的工作,
但是文档中有一个警告:但是:
这取决于个人喜好。我个人更喜欢使用客户端界面,因为它可以更容易理解和跟踪哪些基础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):
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:
The same work can be accomplished with the resource interface
There is a caveat in the documentation, however:
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.