问题描述
我看到我们可以将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云端硬盘中获取了一张照片列表,并将其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 Photos的任何API.
I have searched the docs of Google Script, but I couldn't find any API of Google Photos.
谢谢.
推荐答案
- 您要将Google云端硬盘中的图像文件上传到Google Photo.
- 您想使用Google Apps脚本实现这一目标.
- 图书馆的项目密钥为
1lGrUiaweQjQwVV_QwWuJDJVbCuY2T0BfVphw6VmT85s9LJFntav1wzs9
. -
https://www.googleapis.com/auth/photoslibrary
-
https://www.googleapis.com/auth/script.external_request
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云端硬盘中的文件上传到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 Photo.在此示例脚本中,使用了脚本的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)
}