最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

将文件从S3下载到本地计算机

IT培训 admin 5浏览 0评论

将文件从S3下载到本地计算机

我正在尝试从AWS S3下载音频(mp3)文件到本地计算机。当我在本地主机上执行时,但在将相同的代码部署到AWS之后,它工作正常。它将文件下载到服务器计算机而不是用户的本地计算机。

试过这两个版本。两者都以同样的方式做

版本1:

    const key = track.audio_transcode_filename.substring(20);

    var s3Client = knox.createClient(envConfig.S3_BUCKET_TRACKS);
    const os = require('os');
    const downloadPath = os.homedir().toString();
    const config =require('../../config/environment');
    const fs = require('fs');
    var filePath=downloadPath + "\\Downloads\\" + track.formatted_title + ".mp3";
    if (fs.existsSync(filePath)) {
        var date = new Date();
        var timestamp = date.getTime();
        filePath=downloadPath + "\\Downloads\\" + track.formatted_title + "_" + timestamp + ".mp3";
     }
    const file = fs.createWriteStream(filePath);

    s3Client.getFile(key, function(err, res) {
      res.on('data', function(data) { file.write(data); });
      res.on('end', function(chunk) { file.end(); });
    });

版本2:

  var audioStream = '';

    s3Client.getFile(key, function(err, res) {
      res.on('data', function(chunk) { audioStream += chunk });
      res.on('end', function() { fs.writeFile(filePath + track.formatted_title + ".mp3", audioStream, 'binary')})
    }); 

谢谢,Kanth

回答如下:

您需要将其发送给用户。所以,我认为你有一个expressJS,用户可以使用你的API端点获取元素。

完成所有问题后,您需要将其发送给用户。

res.sendFile('/path/to/downloaded/s3/object')

将文件从S3下载到本地计算机

我正在尝试从AWS S3下载音频(mp3)文件到本地计算机。当我在本地主机上执行时,但在将相同的代码部署到AWS之后,它工作正常。它将文件下载到服务器计算机而不是用户的本地计算机。

试过这两个版本。两者都以同样的方式做

版本1:

    const key = track.audio_transcode_filename.substring(20);

    var s3Client = knox.createClient(envConfig.S3_BUCKET_TRACKS);
    const os = require('os');
    const downloadPath = os.homedir().toString();
    const config =require('../../config/environment');
    const fs = require('fs');
    var filePath=downloadPath + "\\Downloads\\" + track.formatted_title + ".mp3";
    if (fs.existsSync(filePath)) {
        var date = new Date();
        var timestamp = date.getTime();
        filePath=downloadPath + "\\Downloads\\" + track.formatted_title + "_" + timestamp + ".mp3";
     }
    const file = fs.createWriteStream(filePath);

    s3Client.getFile(key, function(err, res) {
      res.on('data', function(data) { file.write(data); });
      res.on('end', function(chunk) { file.end(); });
    });

版本2:

  var audioStream = '';

    s3Client.getFile(key, function(err, res) {
      res.on('data', function(chunk) { audioStream += chunk });
      res.on('end', function() { fs.writeFile(filePath + track.formatted_title + ".mp3", audioStream, 'binary')})
    }); 

谢谢,Kanth

回答如下:

您需要将其发送给用户。所以,我认为你有一个expressJS,用户可以使用你的API端点获取元素。

完成所有问题后,您需要将其发送给用户。

res.sendFile('/path/to/downloaded/s3/object')

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论