Downloading S3 File through UI Action

#1

I have a method to download a S3 file from console: Curl to download datafile from S3 to local drive

I’m struggling with how to implement this through UI. I’ve tried putting this in custom code in UI content, but the S3File type is not recognized, and c3Make does not return methods, so I can’t do readString().

I’ve tried making a js function on a type’s method, but then console is not recognized.

Does anybody have ideas for a solution?

0 Likes

#2

You can try with adding a function to some type that get a path and return the content of the file on S3, e.g.

type S3FileReader {
  read: function(path: string): string js server
}
function read(path) {
  return S3File.make({url: path}).readString();
}

Then have a UI action that calls that function with a path param:

var q = new C3.network.C3Request({
  environment: this.get('environment'),
  c3type: 'S3FileReader',
  async: true,
  //responseSelector: 'objs',
  c3function: 'read',
  c3arguments: {
    path : somepath
  },
  success: function (res) {
     result = res.response.get('rawData');         
     // download the result       
     var a = document.createElement('a');
     a.href = URL.createObjectURL(new Blob([result], {
       type: 'application/octet-stream'
     ));
     var filename = 'content.txt';
     a.setAttribute('download', filename);
     document.body.appendChild(a);
     a.click();
     document.body.removeChild(a);
  },
  failure: function (res) {

  }
});
q.execute();

In 7.8 check this thread 7.8 - C3.network.C3Request raising error

0 Likes

#4

read string is a bad idea on UI if the file is huge.
Can you make a rest call to the File API in your code using file/1/

vanity-url/file/1/tenant/tag/fullUrlOrEncodedPath
1 Like

#5

I ended up using this with a few tweaks:

return S3File.make({url: path}).read();
server was sending the above back as base64, so I converted that to arraybuffer on browser side

Ended up only taking about 3 seconds for a 2000 page pdf, so I’m happy with performance.

0 Likes

#6

File.make(url).read() is better and more general. if the ‘url’ starts with s3:// then the file thats created will be an instance of S3File but will ALSO work for any other file system.

3 Likes