Change the file name - GCP

We are using Google Cloud Storage to store files. is it possible to change the file name to something unique or add something unique in file name to stop replacing old file with same name?

I use AWS S3 buckets, not Google Cloud, so don’t know if below code translates directly, but when preparing the upload URL server side, I prepend a time stamp to the beginning of the file name to avoid the issue of overwriting a file with the same name.

Key: ${Date.now().toString()}-${JSON.parse(Object.keys(req.body)[0]).filename} , // what we'll call our file - prepending a date string to the filename to avoid collisions - S3 will overwrite a file if another with the same key (i.e. name) is uploaded!

(Not sure how to escape characters in this comment system - there should be a back tick before ${Date… and after .filename} )

You could probably also enable versioning - I’m not familiar with how to do that in Google Cloud.

Hope that’s helpful.

Hey, thanks

What do you mean by server side?
I am using Companion stand alone server and providing config via env.

sorry for late reply.

Sorry - I didn’t mean to confuse the issue - the way AWS S3 works is that the browser sends a request to the server for credentials for the upload, and that request includes the file names. The server prepares those credentials, and responds with an upload link to the browser. The browser then initiates the upload directly to S3. When the server receives that credential request with the filenames, there’s an opportunity to amend the file name; in my case, I prepend a timestamp.

In your case, you could try the onBeforeFileAdded option to make your filename unique - see here, where you’ll find sample code like this:

onBeforeFileAdded: (currentFile, files) => {
  const modifiedFile = {
    ...currentFile,
    name: currentFile.name + '__' + Date.now()
  }
  return modifiedFile
}

That would append a timestamp to your file name and, I think, would show the user the appended name.

If you don’t want the user to see the altered name, I think you could use onBeforeUpload - see here:

onBeforeUpload: (files) => {
  // We’ll be careful to return a new object, not mutating the original `files`
  const updatedFiles = {}
  Object.keys(files).forEach(fileID => {
    updatedFiles[fileID] = {
      ...files[fileID],
      name: 'myCustomPrefix' + '__' + files[fileID].name
    }
  })
  return updatedFiles
}

I believe the difference between those two options is that the first option happens when the user has selected the file to upload, but right before it is added to the list of files to upload (i.e. just before it appears on the Dashboard, if you’re using that). The second option happens after the file has been added, but right before the upload. It’s probably the better option if it’s not important for the user to see the changes you make.

You could also use versioning in your Google Cloud, so that objects aren’t overwritten - see here.

Hope that’s helpful.

Thank You so much.

using this logic can we add file into some sub directory also?

like root/website/org_1/file.png

by just changing name key in onBeforeUpload function to /website/org_1/file.png

Yes, that should work, and it’s exactly what I do in the ‘Account_Uploads/’ part of the code below:

Key: `Account_Uploads/${Date.now().toString()}-${JSON.parse(Object.keys(req.body)[0]).filename}`,

(though from the point of view of the cloud, I think it’s just a flat name space, meaning it doesn’t actually create a hierarchy - the files, slashes and all, get stored at the top (and only) level of the bucket - for Google Cloud, it looks like you can use gsutil to interpret a hierarchy based on forward slashes - see here for more info).

Hey, thanks man,

it worked but with small change,

onBeforeUpload: (files) => {
  // We’ll be careful to return a new object, not mutating the original `files`
  const updatedFiles = {}
  Object.keys(files).forEach(fileID => {
    updatedFiles[fileID] = {
      ...files[fileID],
      meta: {
        ...files[fileID].meta,
        name: 'myCustomPrefix' + '__' + files[fileID].meta.name,
     },
    }
  })
  return updatedFiles
}

Again thanks a lot

Glad it worked out for you!

i have the same problem