S3 File Adapter Base url - Can it take function?

GenerateKey can take a function like this

generateKey: (filename) => {
                        return `${Date.now()}_${filename}`; // unique prefix for every filename
                    }

İ want to change baseUrl depending on filename. Can baseUrl field take function?

İf yes, how can I do that?

It is not possible in the current implementation. You can fork it and create your own adapter though. It is not that hard: https://github.com/parse-community/parse-server-s3-adapter/blob/master/index.js

1 Like

Hey. İ looked at the code. İ believe i have to edit getFileLocation function right?

İ don’t have any javascript experience. That’s why I’m asking. İ don’t know where to start

Yes. I believe that would be the right place.

Hi. Today I had a time to change the code. This is final code:

getFileLocation(config, filename) {
    const fileName = filename.split('/').map(encodeURIComponent).join('/');

    if (this._directAccess) {
      if(this._baseUrl){
        if (this._baseUrl instanceof Function) {
          if (this._baseUrlDirect) {
            return `${this._baseUrl(filename)}/${fileName}`;
          }
          return `${this._baseUrl(filename)}/${this._bucketPrefix + fileName}`;
        }
        else{
          if (this._baseUrlDirect) {
            return `${this._baseUrl}/${fileName}`;
          }
          return `${this._baseUrl}/${this._bucketPrefix + fileName}`;
        }
        
      }
      return `https://${this._bucket}.s3.amazonaws.com/${this._bucketPrefix + fileName}`;
    }
    return (`${config.mount}/files/${config.applicationId}/${fileName}`);
  }

Can you look if its ok?

But Only thing I’m confused is what variable ı would pass into baseUrl function? filename from getFileLocation(config, filename) or fileName from const fileName = filename.split('/').map(encodeURIComponent).join('/');

Which one should I pass into this._baseUrl(filename) ?

I’d pass config and filename that are received in the function parameters. I’d also use typeof this._baseUrl === 'function' instead of this._baseUrl instanceof Function. Why don’t you open a PR with this change + a test case and we continue discussing from there?

I created a PR. Here is the discussion issue

I find out we can also discuss changes directly in PR. Sorry for unnecessary issue. I will close it.

This is PR link