Google Apps 脚本:将媒体从 Google Drive 导入(上传)到 Google Photos? [英] Google Apps Scripts: import (upload) media from Google Drive to Google Photos?
问题描述
我发现我们可以将媒体(照片、视频等)从 Google 云端硬盘导入(上传)到 Google 相册.我的目标是:用代码完成这项任务.
I see that we can import (upload) media (photos, videos,...) from Google Drive to Google Photos. My goal is: do this task with code.
我已成功从 Google Drive 中获取照片列表,并将它们的 ID 存储在一个数组中.我有这个代码:
I have successfully got a list of photos from Google Drive, and I store their IDs in an Array. I have this code:
var arrId =
[
//Some image id...
];
var length = arrId.length;
var driveApplication = DriveApp;
for(var i=0;i<length;++i)
{
var file = driveApplication.getFileById(arrId[i]);
//Now I got the file, how can I programmatically import this file to Google Photos
}
我已经搜索了 Google Script 的文档,但我找不到Google 相册的任何 API.
I have searched the docs of Google Script, but I couldn't find any API of Google Photos.
谢谢.
推荐答案
- 您想将 Google Drive 中的图片文件上传到 Google Photo.
- 您希望使用 Google Apps 脚本实现此目的.
- 库的项目密钥是
1lGrUiaweQjQwVV_QwWuJDJVbCuY2T0BfVphw6VmT85s9LJFntav1wzs9
. https://www.googleapis.com/auth/photoslibrary
https://www.googleapis.com/auth/script.external_request
如果我的理解是正确的,那么这个答案呢?
If my understanding is correct, how about this answer?
在这个答案中,我想使用 GPhotoApp 的 Google Apps 脚本库来实现您的目标.该库具有将 Google Drive 中的文件上传到 Google Photo 的方法.
In this answer, I would like to achieve your goal using a Google Apps Script library of GPhotoApp. This library has the method for uploading a file in Google Drive to Google Photo.
关于这一点,您可以在这里查看详细流程.
About this, you can see the detail flow at here.
安装此库.
这个库使用 V8 运行时.所以请在脚本编辑器中启用 V8.
此库使用以下 2 个作用域.在这种情况下,安装库时,会自动安装这些作用域.
This library use the following 2 scopes. In this case, when the library is installed, these scopes are automatically installed.
首先,请检索您要上传的相册 ID.为此,请使用以下脚本.
At first, please retrieve the album ID you want to upload. For this, please use the following script.
function getAlbumList() {
const excludeNonAppCreatedData = true;
const res = GPhotoApp.getAlbumList(excludeNonAppCreatedData);
console.log(res.map(e => ({title: e.title, id: e.id})))
}
示例脚本 2:
使用检索到的相册 ID,您可以将图像文件上传到 Google 相册.在此示例脚本中,使用了您的脚本的 arrId
.
function uploadMediaItems() {
const albumId = "###"; // Please set the album ID.
var arrId =
[
//Some image id...
];
const items = arrId.map(id => {
const file = DriveApp.getFileById(id);
return {blob: file.getBlob(), filename: file.getName()};
});
const res = GPhotoApp.uploadMediaItems({albumId: albumId, items: items});
console.log(res)
}