Amazon AWS S3 强制下载 Mp3 文件而不是流式传输

发布于 2024-09-02 06:05:26 字数 256 浏览 9 评论 0原文

我使用 Amazon S3 放置 mp3 文件,然后允许我们的网站访问者从 Amazon AWS 下载 mp3。我使用 S3Fox 来管理文件,一切似乎都工作正常,直到最近我们收到访客的许多投诉,称 mp3 是通过浏览器传输的,而不是显示浏览器保存对话框。 我尝试播放一些 mp3,注意到对于某些 mp3,会出现保存对话框,而对于其他一些 mp3,它们是通过浏览器进行流式传输。我能做什么来强制下载 mp3 文件而不是通过网络浏览器流式传输......

任何帮助将不胜感激。 谢谢

I'm using Amazon S3 to put the mp3 file then allow our site visitor to download the mp3 from Amazon AWS. I use S3Fox to manage the file, everything seems working fine until recently we got many complaints from visitor that the mp3 was streamed via the browser instead of displaying browser save dialog.
I try for some mp3 and notice that for some mp3, the save dialog box is appear, and for some others they're streamed via browser. What can I do to force that the mp3 file will be downloaded instead of streamed via web browser....

Any help would be much appreciated.
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

他不在意 2024-09-09 06:05:26

为此,您需要设置 Content-Disposition 标头:

Content-disposition: attachment; filename=song.mp3

我认为这对于 S3Fox 是不可能的。您可以使用 Bucket Explorer(非免费)或编写脚本来上传文件。

In order to do so you need to set the Content-Disposition header:

Content-disposition: attachment; filename=song.mp3

I don't think this is possible with S3Fox. You could use Bucket Explorer (not free) or write a script to upload the files.

淡水深流 2024-09-09 06:05:26

好吧,自从你问这个问题以来已经很长时间了,但我遇到了同样的问题,我想与社区分享我的解决方案,以防万一其他人需要解决这个问题。当然,您可以从 Amazon S3 控制台更改 Content-Type 和 Content-Disposition,但有趣的是以编程方式执行此操作。

以下代码对我来说效果很好:

require_once '../sdk-1.4.2.1/sdk.class.php';

// Instantiate the class
$s3 = new AmazonS3();

// Copy object over itself and modify headers
$response = $s3->copy_object(
    array( // Source
        'bucket' => 'your_bucket',
        'filename' => 'Key/To/YourFile'
    ),
    array( // Destination
        'bucket' => 'your_bucket',
        'filename' => 'Key/To/YourFile'
    ),
    array( // Optional parameters
        'headers' => array(
            'Content-Type' => 'application/octet-stream',
            'Content-Disposition' => 'attachment'
        )
    )
);

// Success?
var_dump($response->isOK()); 

希望它可以帮助其他遇到同样问题的人。

Ok, it's been a long time since you ask this, but I had the same problem and I'd like to share my solution with the community, just in case someone else need to solve this thing. Of course, you can change Content-Type and Content-Disposition from the Amazon S3 Console, but the interesting thing is to do it programmatically.

The following code works fine for me:

require_once '../sdk-1.4.2.1/sdk.class.php';

// Instantiate the class
$s3 = new AmazonS3();

// Copy object over itself and modify headers
$response = $s3->copy_object(
    array( // Source
        'bucket' => 'your_bucket',
        'filename' => 'Key/To/YourFile'
    ),
    array( // Destination
        'bucket' => 'your_bucket',
        'filename' => 'Key/To/YourFile'
    ),
    array( // Optional parameters
        'headers' => array(
            'Content-Type' => 'application/octet-stream',
            'Content-Disposition' => 'attachment'
        )
    )
);

// Success?
var_dump($response->isOK()); 

Hope it can helps other struggling with the same trouble.

烟柳画桥 2024-09-09 06:05:26

这最终成为我从 AWS S3 强制下载文件的解决方案。

在 safari 中,文件以 .html 文件的形式下载,直到我停止返回读取文件并单独运行该函数。

  public function get_download($upload_id)
  {
    try {
      $upload = Upload::find($upload_id);
      if ($upload->deleted)
        throw new Exception("This resource has been deleted.");

      if ($upload->filename == '')
        throw new Exception("No downloadable file found.  Please email [email protected] for support.");

      header("Content-Description: File Transfer");
      header("Content-Type: application/octet-stream");
      header("Content-Disposition: attachment; filename={$upload->uploaded_filename};");

      readfile("https://s3.amazonaws.com/stackoverflow/uploads/" . $upload->filename);
      exit;
    } catch(Exception $e) {
      return $e->getMessage();
    }
  }

This ended up being my solution for force downloading files from AWS S3.

In safari the files were downloading as .html files until I stopped returning the readfile and just ran the function alone.

  public function get_download($upload_id)
  {
    try {
      $upload = Upload::find($upload_id);
      if ($upload->deleted)
        throw new Exception("This resource has been deleted.");

      if ($upload->filename == '')
        throw new Exception("No downloadable file found.  Please email [email protected] for support.");

      header("Content-Description: File Transfer");
      header("Content-Type: application/octet-stream");
      header("Content-Disposition: attachment; filename={$upload->uploaded_filename};");

      readfile("https://s3.amazonaws.com/stackoverflow/uploads/" . $upload->filename);
      exit;
    } catch(Exception $e) {
      return $e->getMessage();
    }
  }
临走之时 2024-09-09 06:05:26

在 s3 管理控制台窗口中,右键单击并选择属性。

单击元数据。

单击添加更多元数据

密钥:内容处置
值:附件保存

。就这样。

In s3 management console window, right click and chose properties.

Click on metadata.

Click on add more metadata

Key: content-disposition
Value: attachment

Save. That's all.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文