Skip to content

Instantly share code, notes, and snippets.

@tanaikech
Last active January 28, 2018 09:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tanaikech/78058814cdcbb80207b87e116347a997 to your computer and use it in GitHub Desktop.
Save tanaikech/78058814cdcbb80207b87e116347a997 to your computer and use it in GitHub Desktop.
Google Apps Scriptを使用したGoogle Drive上のファイル移動 ref: http://qiita.com/tanaike/items/1d3b2cac43cf5c34e9f7
var sourcefile = [コピー元ファイル名]; // file name
var destfolder = [コピー先フォルダ名]; // folder name
var destfolder = DriveApp.getFoldersByName(destfolder).next();
var file = DriveApp.getFilesByName(sourcefile).next();
var sourcefolder = file.getParents().next();
destfolder.addFile(file);
sourcefolder.removeFile(file);
var sourcefile = DriveApp.getFilesByName([コピー元ファイル名]).next().getId(); // file name
var destfolder = DriveApp.getFoldersByName([コピー先フォルダ名]).next().getId(); // folder name
var accesstoken = [アクセストークン];
UrlFetchApp.fetch('https://www.googleapis.com/drive/v2/files/' + sourcefile, {
method:'put',
headers: {
'Authorization': 'Bearer ' + accesstoken,
'uploadType' : 'multipart/related'
},
contentType: 'application/json',
payload: JSON.stringify({'parents': [{id: destfolder}]})
});
Drive.Files.update(
{"parents": [{"id": "## Folder ID ##"}]},
"## File ID ##"
)
$ curl -X PUT -H 'Content-Type: application/json' -H 'uploadType: multipart/related' -H 'Authorization: Bearer [アクセストークン]' https://www.googleapis.com/drive/v2/files/[コピー元ファイルID] -d '{"parents": [{"id": "[コピー先フォルダID]"}]}'
$ curl -H 'Content-Type: application/json' -H 'Authorization: Bearer [アクセストークン]' https://www.googleapis.com/drive/v2/files -o list.json
$ jq -r ".items[].title" list.json
ファイル名
$ jq -r ".items[].id" list.json
ファイルID
{
"error": {
"errors": [
{
"domain": "global",
"reason": "insufficientPermissions",
"message": "Insufficient Permission"
}
],
"code": 403,
"message": "Insufficient Permission"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment