簡體   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