0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

GASでgmailの添付ファイルをAzureStorageに送信

Posted at

Postmanなら英語情報あるけど、GASで書いているところがなかったのでメモで残しておく
テスト用スクリプト

var FindSubject = 'subject:(メールで引っこ抜きたいタイトル) ';
 
function getMail(){
 //この辺りはたくさん情報あるのでパクリ
  //指定した件名のスレッドを検索して取得 
  var myThreads = GmailApp.search(FindSubject, 0, 15); 
  //スレッドからメールを取得し二次元配列に格納
  var myMessages = GmailApp.getMessagesForThreads(myThreads);
  // とりあえずGoogleDriveに保存する
  var folder = DriveApp.getFolderById("GoogleドライブのフォルダのURLの最後のところ");

  var finename;

  console.log(myMessages.length);
  
 
  for(var i in myMessages){
    for(var j in myMessages[i]){
 
      //スターがないメッセージのみ処理   
      if(!myMessages[i][j].isStarred()){ 
        
        var strDate = myMessages[i][j].getDate();
        var strSubject = myMessages[i][j].getSubject();
        var strMessage = myMessages[i][j].getPlainBody().slice(0,200);
        var attachments = myMessages[i][j].getAttachments();
        
        // 特に意味ないがGoogleDriveにも保存しておく
        if( attachments.length > 0 ){
          var file = attachments[0];
          filename = file.getName();
          file.setName(filename);
          folder.createFile(file);
          sendFileAzure(file);
        }
 
        //処理済みのメッセージをスターをつける
        myMessages[i][j].star(); 
      }
    }
  }
}

// これがAzureに送信するところ
function sendFileAzure(attachFile){
 // ストレージアカウントのShared Access Signatureで作成出来るURL。コンテナ名とファイル名を忘れずに。今回は固定ファイル名にしている
  var endpoint = "https://ストレージアカウント.blob.core.windows.net/コンテナ名/ファイル名?sv=2021-06-08&ss=bf&srt=o&sp=rwactfx&se=2023-03-31T15:55:29Z&st=2023-02-17T07:55:29Z&spr=https&sig=ほにゃほにゃ";
  
  //post でなく putなのが注意。最低限必要なのはx-ms-blob-type
  x=UrlFetchApp.fetch(endpoint, {
  'headers': {
    'x-ms-blob-type':'BlockBlob'
  },
  'muteHttpExceptions' : true,
  'method': 'PUT',
  'payload':attachFile
  });

  // 201がかえれば成功
  console.log(x.getResponseCode());

}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?