且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Google Apps脚本:将媒体从Google云端硬盘导入(上传)到Google相册吗?

更新时间:2022-11-24 16:24:46

  • 您要将Google云端硬盘中的图像文件上传到Google Photo.
  • 您想使用Google Apps脚本实现这一目标.
  • 如果我的理解是正确的,那么这个答案如何?

    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.

    安装此库.

    • 图书馆的项目密钥为 1lGrUiaweQjQwVV_QwWuJDJVbCuY2T0BfVphw6VmT85s9LJFntav1wzs9 .

    该库使用V8运行时.因此,请在脚本编辑器中启用V8.

    此库使用以下2个作用域.在这种情况下,安装库后,将自动安装这些作用域.

    This library use the following 2 scopes. In this case, when the library is installed, these scopes are automatically installed.

    • 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

    首先,请获取您要上传的相册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)
}

注意:

  • 在我的环境中,我可以确认上述脚本可以正常工作.
    • Photos Library API
    • GPhotoApp