【问题标题】:boto - What exactly is a key?boto - 究竟什么是钥匙?
【发布时间】:2015-06-05 09:00:07
【问题描述】:

正如标题所说,boto 中的 key 是什么?

  • 它封装了什么(字段、数据结构、方法等)?
  • 如何使用 key/boto 访问 AWS 存储桶中文件的文件内容?

我无法在他们的官方文档或任何其他第三方网站上找到此信息。有人可以提供这些信息吗?

以下是key 对象的一些用法示例:

def download_file(key_name, storage):
    key = bucket.get_key(key_name)
    try:
        storage.append(key.get_contents_as_string())
    except:
        print "Some error message."

和:

for key in keys_to_process:
    pool.spawn_n(download_file, key.key, file_contents)
pool.waitall()

【问题讨论】:

    标签: python-2.7 amazon-web-services amazon-s3 boto


    【解决方案1】:

    在您的代码示例中 - 键是对存储桶内唯一标识符的对象引用。

    将存储桶视为数据库中的表 将键视为表中的行 您引用存储桶中的密钥(更好地称为对象)。

    在boto(不是boto3)中经常这样工作

    from boto.s3.connection import S3Connection
    connection = S3Connection()  # assumes you have a .boto or boto.cfg setup
    bucket = connection.get_bucket('my_bucket_name_here') # this is like the table name in SQL, select OBJECT form TABLENAME
    key = bucket.get_key('my_key_name_here') this is the OBJECT in the above SQL example.  key names are a string, and there is a convention that says if you put a '/' in the name, a viewer/tool should treat it like a path/folder for the user, e.g.  my/object_name/is_this is really just a key inside the bucket, but most viewers will show a my folder, and an object_name folder, and then what looks like a file called is_this simply by UI convention
    

    【讨论】:

      【解决方案2】:

      由于您似乎在谈论简单存储服务 (S3),您可以在 S3 文档的第 1 页找到该信息。

      每个对象都使用开发人员分配的唯一密钥进行存储和检索。

      键是存储桶中对象的唯一标识符。桶中的每个对象都只有一个键。由于存储桶、键和版本 ID 的组合唯一地标识了每个对象,因此可以将 Amazon S3 视为“存储桶 + 键 + 版本”与对象本身之间的基本数据映射。 Amazon S3 中的每个对象都可以通过 Web 服务终端节点、存储桶名称、密钥和可选版本的组合来唯一寻址。例如,在 URL http://doc.s3.amazonaws.com/2006-03-01/AmazonS3.wsdl 中,“doc”是存储桶的名称,“2006-03-01/AmazonS3.wsdl”是键。

      http://docs.aws.amazon.com/AmazonS3/latest/dev/Introduction.html

      键只是一个字符串——存储桶中对象的“路径和文件名”,没有前导/

      【讨论】:

      • 您好,很抱歉回复晚了。那是一个 s3 键,但在两者中,键似乎都是对象,因为它们具有与它们相关的其他方法。
      • 你的问题很不清楚。请编辑问题并添加一些代码示例来说明您已经看到和询问的内容。
      • 根据要求,请参阅修改后的帖子 Micheal
      猜你喜欢
      • 1970-01-01
      • 2011-02-01
      • 2014-10-28
      • 2012-08-27
      • 2010-11-12
      • 2011-03-18
      • 2011-01-22
      • 1970-01-01
      相关资源
      最近更新 更多