DEV Community

Kamal Hossain
Kamal Hossain

Posted on

Upload file to Google cloud storage without middleman server

In my previous blog I discussed about how to upload files from Node.js server to Google Cloud Storage. So, in that case users have to upload the data to the Node.js server and then we are uploading the data to the Google Cloud Storage server.

So, think for a second, if a user wants to upload a very large file and then if we want to upload that large content to the Google Cloud Storage then we have to handle the large amount of data in our server.

So what we can do, we can give some kind of authorization to the user to upload that particular file directly from their device. Without passing the data to our server. So for that we can create an endpoint to our server and then provide a Signed Url to the user. In that way, a user can use this signed url for a limited amount of time to upload his content to the Google Cloud Platform.

In my case I will use a Node.js server for that. We just have to create an index.js file like this one.

const http = require('http')
const { Storage } = require('@google-cloud/storage')

const storage = new Storage({
  keyFilename: `./key-file-from-service-account.json`,
})

const bucketName = 'my-test-bucket'

const server = http.createServer(async (req, res) => {
  // Handle GET requests
  if (req.method === 'GET') {
    res.writeHead(200, { 'Content-Type': 'text/plain' })

    const fileName = `uploadedFileName.jpeg`

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

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

    res.end(url)
  }
})

const port = 3000
server.listen(port, () => {
  console.log(`Server running on port ${port}`)
})

Enter fullscreen mode Exit fullscreen mode

Here above we are initializing the bucket with @google-cloud/storage package.

Then inside the GET request we are calling the getSignedUrl() to get the signed url with some predefined config above. That's it now with the URL user can upload the content from any device they want.

Here I am not doing any kind of authorization for our users. But in real life scenarios we must validate the user with some kind of authentication to make sure not everyone gets the Signed url.

Top comments (0)