Upload image to Google Cloud Storage

Hello, Im working on a home project with the AMB82 mini. Thanks for developing this great board. The goal of the project is to 1) Take an image with the camera. 2) Upload the image to Google Could Storage bucket folder and 3) Retrieve this image when necessary to do some post processing. Im by no means an expert, but using some of the examples built-in, I was able to do #1 (Which was pretty straightforward). However, Im unable to upload image to Google Cloud Storage bucket (the examples provided have code to upload to Google Drive, which I was able to replicate in my project). Does anyone have code that can upload the image to Google Could Storage bucket using the API? Thank you so much for you help in advance :)!

Hi @daddys.organic ,

May I know which SDK are you using and do you mind to share the example code that you mentioned?

Thank you.

Hello. Thanks for the response. For software, im using Arduino IDE Version 2.3.2 (i think thats the latest version). For example code i.e. startup camera, take image and upload to Google Drive i’m using parts of the MotionDetectGoogleLineNotify example code. I’m able to the capture an image and upload it Google Drive. But what I need for my project is to upload the image to Cloud Storage (i.e. GCS or AWS, but prefer GCS). Any help is greatly appreciated.

Hi @daddys.organic ,

You might need to modify your code on Google Apps Script to do a HTTP post request to GCS with authorization token included in the header. Here is the example code that I tested to upload the image taken to GCS in addition to Google Drive and Line Notify.

function doPost(e) {
    // Retrieve variables from the POST requests
    var myFoldername = e.parameter.myFoldername;
    var myFile = e.parameter.myFile;
    var myFilename = Utilities.formatDate(new Date(), "GMT", "yyyyMMddHHmmss")+"-"+e.parameter.myFilename;
    var myToken = e.parameter.myToken;

    // Store the file type and Base64 encoded data
    var contentType = myFile.substring(myFile.indexOf(":")+1, myFile.indexOf(";"));
    var data = myFile.substring(myFile.indexOf(",")+1);
    data = Utilities.base64Decode(data);
    var blob = Utilities.newBlob(data, contentType, myFilename);

    // Save a captured image to Google Drive.
    var folder, folders = DriveApp.getFoldersByName(myFoldername);
    if (folders.hasNext()) {
        folder = folders.next();
    } else {
        folder = DriveApp.createFolder(myFoldername);
    }
    var file = folder.createFile(blob);
    file.setDescription("Uploaded by " + myFilename);

    var imageID = file.getUrl().substring(file.getUrl().indexOf("/d/")+3,file.getUrl().indexOf("view")-1);
    var imageUrl = "https://drive.google.com/uc?authuser=0&id="+imageID;
      
    // Send a link message to Line Notify.
    var res = "Line Notify: ";
    try {
      var url = 'https://notify-api.line.me/api/notify';
      var response = UrlFetchApp.fetch(url, {
        'headers': {
          'Authorization': 'Bearer ' + myToken,
        },
        'method': 'post',
        'payload': {
            'message': imageUrl
        }
      });
      res += response.getContentText();
    } catch(error) {
      res += error;
    } 

    // Upload the image to Google Cloud Storage
    var cloudRes = uploadToGoogleCloudStorage(blob, myFilename, contentType);

    // Returning Results
    return ContentService.createTextOutput(myFoldername+"/"+myFilename+"\n"+imageUrl+"\n"+res+"\n"+cloudRes);
}
// Helper function to upload the file to Google Cloud Storage using OAuth Token
function uploadToGoogleCloudStorage(blob, myFilename, contentType) {
    var bucketName = 'bucket-name';  // Replace with your Cloud Storage bucket name
    var OAuthToken = 'authorization-token';  // Replace with your generated OAuth Token

    var url = 'https://storage.googleapis.com/upload/storage/v1/b/' + bucketName + '/o?uploadType=media&name=' + encodeURIComponent(myFilename);
    
    try {
        var options = {
          'headers': {
            'Authorization': 'Bearer ' + OAuthToken,
            'Content-Type': contentType
          },
          'method': 'POST',
          'payload': blob.getBytes()  // Send the blob data
        };

        var response = UrlFetchApp.fetch(url, options);
        var responseData = JSON.parse(response.getContentText());

        var fileUrl = 'https://storage.googleapis.com/' + bucketName + '/' + myFilename;
        return 'Cloud Storage: Successfully uploaded at ' + fileUrl;
    } catch (e) {
        return 'Cloud Storage: Error uploading - ' + e.message;
    }
}

Thanks.

References: