0

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

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

4

1 回答 1

0

有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 上运行。

于 2020-07-13T09:25:02.557 回答