如何使用javascript将文件从谷歌驱动器上传到服务器

时间:2017-11-08 06:06:42

标签: javascript google-drive-api google-api-php-client

我想将文件从谷歌硬盘上传到S3服务器。请问您能分享我的想法和建议吗?

1 个答案:

答案 0 :(得分:0)

$fileId = '0BwwA4oUTeiV1UVNwOHItT0xfa2M';
$response = $driveService->files->get($fileId, array(
'alt' => 'media'));
$content = $response->getBody()->getContents();

这是从Google云端硬盘下载文件的代码。 参考:https://developers.google.com/drive/v3/web/manage-downloads

因此,您需要使用AWS SDK将其上传到s3

use Aws\Common\Exception\MultipartUploadException;
use Aws\S3\Model\MultipartUpload\UploadBuilder;
$uploader = UploadBuilder::newInstance()
    ->setClient($client)
    ->setSource('/path/to/large/file.mov')
    ->setBucket('mybucket')
    ->setKey('my-object-key')
    ->setOption('Metadata', array('Foo' => 'Bar'))
    ->setOption('CacheControl', 'max-age=3600')
    ->build();

// Perform the upload. You can abort the upload if something goes wrong
try {
    $uploader->upload();
    echo "Upload complete.\n";
} catch (MultipartUploadException $e) {
    $uploader->abort();
    echo "Upload failed.\n";
}

参考:http://docs.aws.amazon.com/aws-sdk-php/v2/guide/service-s3.html#uploading-large-files-using-multipart-uploads