amazon-web-services - 为 Amazon S3 中的小大小写文件创建大小写重定向

标签 amazon-web-services amazon-s3 batch-rename

情况如下:

我在 Amazon S3 上有一个静态网站托管。里面的所有文件都是小写字母,例如:file.html

我正在寻找一个脚本/程序/工具来查找 S3 站点中的所有小写字母文件并创建多个大小写 301 重定向。

例如创建 File.htmlFILE.html 两个文件,并使用新的 301 重定向功能将大写字母的请求重定向到小写字母的真实文件。

请指教

最佳答案

我已经编写了一个可以实现您想要的功能的脚本。它无论如何都不是很圆润,但应该可以解决问题。我已将其放在 GitHub 上:https://github.com/mikewirth/s3-caseredirect .

用法: python makeredirects.py access_code secret 存储桶名称 key_for_your_file

我尝试了一个使用重定向规则功能的版本,但它不起作用,因为有大约 20 条规则的限制。因此,该脚本将创建大量空键。


为了完整起见,并且由于它太小,这里是脚本:

#!/usr/bin/env python

"""
This script takes a file on S3 and creates a redirect from every possible
permutation of case to the original file.

Author: Michael Wirth (https://github.com/mikewirth/s3-caseredirect/)
"""

import sys
import os.path
import argparse

try:
    import boto.s3.connection

except:
    print "boto library (http://code.google.com/p/boto/) for aws needs to be installed"
    sys.exit(1)

filenames = None


def make_case_insensitive(bucket, access, secret, key):
    """ Get filename permutations """
    global filenames
    filenames = []
    filename = os.path.basename(key)
    path = os.path.dirname(key)

    filename_permutations(filename)

    connection = boto.s3.connection.S3Connection(access, secret, True)
    b = connection.get_bucket(bucket)

    for fname in filenames:
        if fname == filename:
            continue

        k = b.new_key(os.path.join(path, fname))
        k.set_redirect(key)


def filename_permutations(filename, pos=0):
    if len(filename) == pos:
        filenames.append(filename)
    else:
        upper = filename[:pos] + filename[pos:pos+1].upper() + filename[pos+1:]
        lower = filename[:pos] + filename[pos:pos+1].lower() + filename[pos+1:]

        if upper != lower:
            filename_permutations(upper, pos+1)
            filename_permutations(lower, pos+1)
        else:
            filename_permutations(filename, pos+1)


def main():
    """ CLI """
    parser = argparse.ArgumentParser()

    parser.add_argument("access", help="AWS credentials: access code")
    parser.add_argument("secret", help="AWS credentials: secret")
    parser.add_argument("bucket", help="Name of Amazon S3 bucket")
    parser.add_argument("key", help="Name of the key to make available case-insensitively. (Starts with a slash.)")

    args = parser.parse_args()

    make_case_insensitive(args.bucket, args.access, args.secret, args.key)

if __name__ == "__main__":
    main()

关于amazon-web-services - 为 Amazon S3 中的小大小写文件创建大小写重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14675713/

相关文章:

amazon-web-services - 如何解决 AWS CloudFormation 不一致的属性 AttributeDefinitions?

amazon-web-services - 将音频分解成许多s3对象的最佳方法是什么?

node.js - 直接从 Lambda/tmp 文件夹播放音频

php - JMESPATH 可以执行 "contains X or Y"搜索/过滤吗?

linux - 使用 bash 在连字符之间批量重命名文件名

amazon-ec2 - 在 AWS 微实例上运行 Chrome

django - 使用预配置的 Docker 容器进行 Elastic Beanstalk Django 部署

ios - iOS SDK v2 的 Amazon S3 视频上传问题

bash - 使用数学运算重命名文件

python - 使用 .rename 和 .endswith 重命名多个图像