简体   繁体   English

从 EC2 到 S3 的文件迁移

[英]File Migration from EC2 to S3

We are currently creating a website that is kind of an upgrade to an old existing one.我们目前正在创建一个网站,该网站是对现有旧网站的升级。 We would like to keep the old posts (that include images) in the new website.我们希望在新网站中保留旧帖子(包括图片)。 The old files are kept in an ec2 instance while the new website is serverless and keeps all it's files in s3.旧文件保存在 ec2 实例中,而新网站是无服务器的,并将所有文件保存在 s3 中。

My question, is there any way I could transfer the old files (from ec2) to the new s3 bucket using Python.我的问题是,有什么方法可以使用 Python 将旧文件(从 ec2)传输到新的 s3 存储桶。 I would like rename and relocate the files in the new filename/filepathing pattern that we devs decided.我想重命名和重新定位我们开发人员决定的新文件名/文件路径模式中的文件。

There is boto3, python aws toolkit.有 boto3、python aws 工具包。

https://boto3.amazonaws.com/v1/documentation/api/latest/guide/s3-uploading-files.html 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

You can write script with S3 upload_file function, then run on your ec2 locally.您可以使用 S3 upload_file function 编写脚本,然后在本地 ec2 上运行。

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

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