【问题标题】:Check local files vs what's on S3, and upload any that have changed检查本地文件与 S3 上的内容,并上传任何已更改的内容
【发布时间】:2018-04-24 07:35:24
【问题描述】:

我正在使用此 python 脚本将已更改或新创建的文件从本地文件夹上传到 S3 文件夹。

脚本不工作。它只是无法获取存储桶名称。我在 python2.7 中使用 boto。我已经用谷歌搜索但无法得到答案。

非常感谢任何帮助。

非常感谢。

这是错误

Traceback (most recent call last):
 File "s3update.py", line 20, in <module>
 bucket = conn.get_bucket(BUCKET_NAME)
 File "/usr/lib/python2.7/site-packages/boto/s3/connection.py", line 506, in get_bucket
return self.head_bucket(bucket_name, headers=headers)
File "/usr/lib/python2.7/site-packages/boto/s3/connection.py", line 539, in head_bucket
raise err
boto.exception.S3ResponseError: S3ResponseError: 403 Forbidden

这里是代码

#!/usr/bin/env python

# Compare a file on S3 to see if we have the latest version  
# If not, upload it and invalidate CloudFront

import fnmatch
import os
import boto
import pprint
import re
import hashlib
from boto.s3.key import Key

# Where source is checked out
SOURCE_DIR  = '/Downloads/local/folder'
BUCKET_NAME = 's3bucket'

# Connect to S3 and get bucket
conn = boto.connect_s3()
bucket = conn.get_bucket('s3bucket')

# Shortcut to MD5
def get_md5(filename):
 f = open(filename, 'rb')
 m = hashlib.md5()
 while True:
   data = f.read(10240)
   if len(data) == 0:
     break
   m.update(data)
 return m.hexdigest()

def to_uri(filename):
  return re.sub(SOURCE_DIR, '', f)

# Assemble a list of all files from SOURCE_DIR
files = []
for root, dirnames, filenames in os.walk(SOURCE_DIR):
  for filename in filenames:
     files.append(os.path.join(root, filename))

# Compare them to S3 checksums
files_to_upload = []
for f in files:
  uri = to_uri(f)
  key = bucket.get_key(uri)
  if key is None:
  # new file, upload
    files_to_upload.append(f)
  else:
  # check MD5
    md5  = get_md5(f)
    etag = key.etag.strip('"').strip("'")
    if etag != md5:
      print(f + ": " + md5 + " != " + etag)
      files_to_upload.append(f)

  # Upload + invalidate the ones that are different
for f in files_to_upload:
    uri = to_uri(f)
    key = Key(bucket)
    key.key = uri
    key.set_contents_from_filename(f)
    # CloudFront invalidation code goes here

【问题讨论】:

  • 它与权限有什么关系吗?
  • 权限看起来没问题。因为我可以使用 Boto3 获取存储桶名称。但我不知道如何用 boto3 修改这个脚本。
  • 只有bucket创建者拥有添加对象的全部权限。您必须授予其他访问密钥的权限。
  • 是否有理由不使用 aws s3 sync cli 命令?

标签: python python-2.7 boto


【解决方案1】:

您应该确保您的存储桶名称正确且存在,并且您有权访问它。

【讨论】:

  • 我拥有存储桶的编辑权限。因为我可以通过 FTPclient 在存储桶中创建文件夹。
  • 当我遇到这个问题时,这解决了我的问题,这是我必须为 AWS 指定的一个愚蠢的权限问题。在这里查看答案stackoverflow.com/a/10884964/3140312
【解决方案2】:

我遇到了与此类似的错误,这是因为虽然我的用户权限确实允许对存储桶内的任何内容进行所有访问,但它不允许在存储桶级别执行操作,例如在其中列出文件/对象。

对我来说,在运行最初读取的策略生成器后,在存储桶权限选项卡上 {

...
        "Action": "s3:*",
        "Resource": "arn:aws:s3:::py-sync-s3/*"
        ]
...

我也不得不改变它:

...
        "Action": "s3:*",
        "Resource": [
            "arn:aws:s3:::py-sync-s3",
            "arn:aws:s3:::py-sync-s3/*"
        ]
...

py-sync-s3 是存储桶名称。

我知道操作 理想情况下,您不希望允许所有带有 * 的操作,这只是为了测试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-17
    • 2011-04-08
    相关资源
    最近更新 更多