How to upload file to google cloud storage bucket using signedUrl


Ghizlane Lotfi

I am developing an Angular app to display the contents of a Google cloud storage bucket. In the back I am using Google Cloud functions in nodeJS

As they mentioned in the documentation to upload the file, I created a function to generate the signed url, but when I send the file with the signed url, the browser gives an error saying error

I tested with postman and it uploaded an empty file

Here is my lambda function:

// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');

// Creates a client
const storage = new Storage();

exports.generateSignedUrl = (req, res) => {
// generate signed url to use for file upload

const filename = req.query.fileName;
console.log('filename ', filename);

const filetype = req.query.fileType;
console.log('filetype ', filetype);

const bucketName = 'nx-terega-omega';

res.set('Access-Control-Allow-Origin', "*");
res.set('Access-Control-Allow-Headers', "Origin, X-Requested-With, 
Content-Type, Accept, Authorization");

if (req.query.fileName !== null && req.query.fileName !== undefined
  && req.query.fileType !== null && req.query.fileType !== undefined) 
{
generateV4UploadSignedUrl(bucketName, filename).then(function (value) 
{
  console.log('File Url response ', value);
  res.status(200).send(JSON.stringify({'url': value}));
}).catch(error => {
  res.status(404).send('Error while generating signed url');
});
} else {
res.status(500).send('Filename not found');
}
};

async function generateV4UploadSignedUrl(bucketName, filename, filetype) {
// [START storage_generate_upload_signed_url_v4]

// These options will allow temporary uploading of the file with outgoing
// Content-Type: application/octet-stream header.
const options = {
version: 'v4',
action: 'write',
expires: Date.now() + 15 * 60 * 1000, // 15 minutes
contentType: filetype,
};

// Get a v4 signed URL for uploading file
const [url] = await storage
.bucket(bucketName)
.file(filename)
.getSignedUrl(options);

console.log('Generated PUT signed URL:');
console.log(url);
console.log('You can use this URL with any user agent, for example:');
console.log("curl -X PUT -H 'Content-Type: application/octet-stream' " +`--upload-file my-file '${url}'`);

return url;
// [END storage_generate_upload_signed_url_v4]
}

When I receive the signed URL, it is sent to my file, but it returns

No 'Access-Control-Allow-Origin' header is present on the requested resource.
Ghizlane Lotfi

As mentioned in Brandon Yarbrough's response, I had to configure cors in Google Cloud. what is missing in my config

[
 {
  "origin": ["http://example.appspot.com"],
  "responseHeader": ["*"],
  "method": ["GET", "HEAD", "DELETE", "PUT"],
  "maxAgeSeconds": 3600
 }
]

You should add PUTin methodand because it's not enough*responseHeaderContent-Type

Related


How to upload file to google cloud storage bucket using signedUrl

Ghizlane Lotfi I am developing an Angular app to display the contents of a Google cloud storage bucket. In the back I am using Google Cloud functions in nodeJS As they mentioned in the documentation to upload the file, I created a function to generate the sign

Upload files to Google Cloud Storage Bucket using PHP and Ajax

Abdul Rehman| I'm working on a PHP project where I need to upload a file (served from an HTML form) to a Google Cloud Storage Bucket. The form request will be received as an ajax request. My file upload is successful, but in the bucket, it shows a temp file, b

Upload files to Google Cloud Storage Bucket using PHP and Ajax

Abdul Rehman| I'm working on a PHP project where I need to upload a file (served from an HTML form) to a Google Cloud Storage Bucket. The form request will be received as an ajax request. My file upload is successful, but in the bucket, it shows a temp file, b

google storage cloud upload file using php

AssemblyX Are there any cloud services that automate uploads via FTP via PHP? I would like to use Google Cloud Storage if possible. What I'm trying to achieve is to run a cron mysql-dumpfunction that automatically moves the resulting files to cloud storage on

How to upload files to Google Cloud Storage Bucket subdirectory

Chris He USA I want to upload a file to a google cloud storage bucket subdirectory using Go. The only reference code I found is this link . wc := client.Bucket(bucket).Object(object).NewWriter(ctx) This objectis your filename string, but filepaths are not all

How to limit file types for Google Cloud Storage bucket?

Encoder 1000 How to limit file types for Google Cloud Storage bucket? For example, how can I choose a bucket and define precisely that it can only contain jpeg, jpg or png and should reject any other type of files? Germod See Policy Document Conditions for For