繁体   English   中英

从 EC2 到 S3 的文件迁移

[英]File Migration from EC2 to S3

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

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

有 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 function 编写脚本,然后在本地 ec2 上运行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM