Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

"cloudstorage not defined" error

Moving Google Alerts from Gmail account to Google storage to be scraped for keywords. Script appears to run, then get error message "CloudStorage Not Defined". Can't get past it. Looked at all APis to be enabled that have to do with storage. Got a Service Account with credentials all that stuff. Got a Project Name, Project ID and Project #. 

0 3 175
3 REPLIES 3

Hi @69Letchworth

Welcome to Google Cloud Community!

The "CloudStorage Not Defined" error usually means that your script doesn't recognize the CloudStorage object. This might be because the required library isn't imported, the Google Cloud Storage API isn't turned on, or there might be an issue with authentication. Also, your “CloudStorage” object might be referring to a bucket storage, your bucket may have not been created. Here are some steps to help you resolve your error:

  • Import the Correct Library: 
    • If you're using Google Apps Script, you might need to enable the Google Cloud Storage service in your Apps Script project. Go to "Resources" -> "Cloud Platform project..." and make sure the correct project is linked.
    • If you're using Python, make sure you have the Google Cloud Storage library installed (pip install google-cloud-storage) and that you're importing it correctly in your script:
  • Enable the Google Cloud Storage API:
    • Go to the Google Cloud Console.
    • Select your project.
    • Navigate to "APIs & Services" -> "Enabled APIs & Services".
    • Search for "Cloud Storage API" and enable it if it's not already enabled.
  • Check Service Account Permissions:
    • In the Google Cloud Console, go to "IAM & Admin" -> "Service Accounts".
    • Find the service account you're using.
    • Click on the "Permissions" tab.
    • Make sure the service account has the "Storage Object Admin" role (or a role with equivalent permissions). You can edit the service account to add this role if it's missing. See documentation on creating and managing service accounts.
  • Set Up Authentication:
    • Make sure the GOOGLE_APPLICATION_CREDENTIALS environment variable is set correctly. This variable should point to the JSON file containing your service account credentials.
    • If you're using Google Apps Script and have linked your Cloud Project, authentication should be handled automatically.
  • Check if your the Storage Bucket exists:
    • Check the Cloud Storage Buckets page in the console to see if your bucket “CloudStorage” appears there.

Was this helpful? If so, please accept this answer as “Solution”. If you need additional assistance, reply here within 2 business days and I’ll be happy to help.

Think I hit the wrong reply button:

Thank you for this. I think I have most of this covered. Where do I insert
the GOOGLE APPLICATION CREDENTIALS. I've got the JSON file and have
inserted but no luck with running it. If I may, I'd like to share what I ve
done so far and get your feedback. Gemini and ChatGPT aren't solving my
problems yet.

function saveGoogleAlertsToCloudStorage() {
// 1. Access Gmail and find Google Alert emails
var threads = GmailApp.search('label:Google Alerts1'); // Use your actual
label

for (var i = 0; i < threads.length; i++) {
var messages = threads[i].getMessages();
for (var j = 0; j < messages.length; j++) {
var message = messages[j];

// 2. Extract data from the email
var subject = message.getSubject();
var body = message.getBody();
// (Use regular expressions or other methods to extract specific data
like links, titles, etc.)

// 3. Prepare the data for Cloud Storage (e.g., as a JSON string)
var data = JSON.stringify({
subject: subject,
body: body,
//... other extracted data
});

// 4. Upload to Google Cloud Storage
var bucketName = 'new-bucket030525'; // Replace with your bucket name
var fileName = 'alert_22125' + message.getId() + '22125.json'; //
Unique filename
SpreadsheetApp.getActiveSpreadsheet().toast('Saving alert to Cloud
Storage: ' + fileName);
saveToCloudStorage(bucketName, fileName, data);
}
}
}

function saveToCloudStorage(bucketName, fileName, data) {
// Get the Cloud Storage service and bucket
var bucket = DriveApp.getStorageFolder(bucketName);

// Create a new blob (file) in the bucket
var blob = bucket.createFile(fileName, data, MimeType.JSON);
}


--

[image: Email Signature Button (2).jpg]
<>
John Krisulewicz
Data Analyst, DonorSearch
(PII Removed by Staff)

(URL Removed by Staff)

Have not heard back from Joy_S regarding my questions. -John