0

I try to download file from AWS S3, this file downloaded successfuly, but it isn't save in my disk, and I don't get any error.

<script>
import AWS from 'aws-sdk';
import FS from 'browserify-fs';

export default {
  computed: {
    s3Client() {
      // Enter copied or downloaded access ID and secret key here
      const ID = 'RANDOM_ID';
      const SECRET = 'RANDOM_SECRET';

      return new AWS.S3({
        accessKeyId: ID,
        secretAccessKey: SECRET
      })
    }
  },
  methods: {
    downloadFile() {
      const BUCKET_NAME = 'js-bucket'
      const FILE_NAME = 'test.epub'

      const params = {
        Key: FILE_NAME,
        Bucket: BUCKET_NAME
      }

      console.info("Try to download file : " + FILE_NAME + " from bucket: " + BUCKET_NAME)

      this.isDownloading = true

      this.s3Client.getObject(params)
      .promise()
      .then(data => {

        FS.writeFile(FILE_NAME, data.Body, function (err) {
          if (err) {
            console.log(err.code, "-", err.message);
          }
        })

        console.log('File downloaded successfully')
      })
      .catch(err => console.error(err))
    }
  }
}

</script>

In console I see my message about success save:File downloaded successfully. But it isn't right.

kyun
  • 9,710
  • 9
  • 31
  • 66
Adam
  • 486
  • 1
  • 7
  • 19

1 Answers1

0

I decided it by myself:

  .promise()
  .then(data => {
    const blob = new Blob([data])
    const link = document.createElement('a')
    link.href = URL.createObjectURL(blob)
    link.download = FILE_NAME
    link.click()
    URL.revokeObjectURL(link.href)

    console.log('File downloaded successfully')
  })

It work, but if anybody know way to do it better, answer please.

Adam
  • 486
  • 1
  • 7
  • 19