使用应用程序脚本将图像从谷歌驱动器上传到亚马逊 S3

Gop*_*mar 5 amazon-s3 google-apps-script google-drive-api

我正在将谷歌表单集成到我们的后端系统中。在表单中,我们接受谷歌驱动器上的图像。每当提交表单时,我都试图将谷歌驱动器图像移动到 s3。 我正在使用它从谷歌驱动器获取图像。

var driveFile = DriveApp.getFileById("imageId"); 
Run Code Online (Sandbox Code Playgroud)

为了将图像上传到 S3,我使用了应用程序脚本库S3-for-Google-Apps-Script。文件正在上传到 s3,但格式不正确。

将图像上传到 S3 的代码是

var s3 = S3.getInstance(awsAccessKeyId, awsSecretKey);
s3.putObject(bucket, "file name", driveFile.getBlob(), {logRequests:true});
Run Code Online (Sandbox Code Playgroud)

从 s3 下载后我无法打开图像。出现错误“它可能已损坏或使用了预览无法识别的文件格式。

提前致谢。

Alb*_*ews -2

首先执行 pip install boto3 googledrivedownloader 请求,然后使用下面给出的代码:

import boto3
from google_drive_downloader import GoogleDriveDownloader as gdd
import os


ACCESS_KEY = 'get-from-aws'
SECRET_KEY = 'get-from-aws'
SESSION_TOKEN = 'not-mandatory'
REGION_NAME = 'ap-southeast-1'
BUCKET = 'dev-media-uploader'


def drive_to_s3_download(drive_url) :
    if "drive.google" not in drive_url:
        return drive_url    #since its not a drive url.

    client = boto3.client(
        's3',
        aws_access_key_id=ACCESS_KEY,
        aws_secret_access_key=SECRET_KEY,
        region_name=REGION_NAME,
        aws_session_token=SESSION_TOKEN  # Optional
    )
    file_id = drive_url.split('/')[5]
    print(file_id)
    gdd.download_file_from_google_drive(file_id=file_id,
                                        dest_path=f'./{file_id}.jpg',
                                        unzip=True)
    client.upload_file(Bucket=BUCKET, Key=f"{file_id}.jpg", Filename=f'./{file_id}.jpg')
    os.remove(f'./{file_id}.jpg')
    return f'https://{BUCKET}.s3.amazonaws.com/{file_id}.jpg'


client = boto3.client(
        's3',
        aws_access_key_id=ACCESS_KEY,
        aws_secret_access_key=SECRET_KEY,
        region_name=REGION_NAME,
        aws_session_token=SESSION_TOKEN  # Optional
    )
# client.download_file(Bucket='test-bucket-drive-to-s3-upload', Key=, Filename=f'./test.jpg')
print(drive_to_s3_download('https://drive.google.com/file/d/1Wlr1PdAv8nX0qt_PWi0SJpx0IYgQDYG6/view?usp=sharing'))
Run Code Online (Sandbox Code Playgroud)

上面的代码将驱动器文件下载到本地,然后上传到S3,然后返回S3 url,任何人都可以根据权限查看该文件。