【问题标题】:File Migration from EC2 to S3从 EC2 到 S3 的文件迁移
【发布时间】:2020-11-02 11:12:35
【问题描述】:

我们目前正在创建一个网站,该网站是对现有旧网站的升级。我们希望在新网站中保留旧帖子(包括图片)。旧文件保存在 ec2 实例中,而新网站是无服务器的,并将所有文件保存在 s3 中。

我的问题是,有什么方法可以使用 Python 将旧文件(从 ec2)传输到新的 s3 存储桶。我想以我们开发人员决定的新文件名/文件路径模式重命名和重新定位文件。

【问题讨论】:

标签: python amazon-s3 amazon-ec2 chalice


【解决方案1】:

有boto3,python aws工具包。

https://boto3.amazonaws.com/v1/documentation/api/latest/guide/s3-uploading-files.html

import logging
import boto3
from botocore.exceptions import ClientError


def upload_file(file_name, bucket, object_name=None):
    """Upload a file to an S3 bucket

    :param file_name: File to upload
    :param bucket: Bucket to upload to
    :param object_name: S3 object name. If not specified then file_name is used
    :return: True if file was uploaded, else False
    """

    # If S3 object_name was not specified, use file_name
    if object_name is None:
        object_name = file_name

    # Upload the file
    s3_client = boto3.client('s3')
    try:
        response = s3_client.upload_file(file_name, bucket, object_name)
    except ClientError as e:
        logging.error(e)
        return False
    return True

您可以使用 S3 upload_file 函数编写脚本,然后在您的 ec2 本地运行。

【讨论】:

    猜你喜欢
    • 2010-12-12
    • 1970-01-01
    • 2021-11-29
    • 2015-08-11
    • 2013-11-07
    • 1970-01-01
    • 2021-06-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多