简体   繁体   English

链接时自动下载 S3 图像

[英]S3 Images being downloaded automatically when linked

What permission am I missing or is this something else?我缺少什么许可或者这是其他什么?

We are using S3 for storage and to serve images to a site with moderate traffic.我们正在使用 S3 进行存储,并将图像提供给流量适中的站点。 We built out a custom thumbnail slider that links a small thumbnail image to a larger slider image at different resolution.我们构建了一个自定义缩略图 slider,它将一个小缩略图链接到一个更大的 slider 不同分辨率的图像。

Before S3 came into play the images would link to each other.在 S3 发挥作用之前,图像会相互链接。 Now once the thumbnail is clicked that image is downloaded automatically rather than just linking to the larger image.现在,一旦单击缩略图,该图像就会自动下载,而不仅仅是链接到更大的图像。 Any thoughts?有什么想法吗?

Here is the code, but this is just an S3 question really.这是代码,但这实际上只是一个 S3 问题。 Thanks!谢谢!

<div class="thumbnails" itemscope itemtype="http://schema.org/ImageGallery">
    <ul id="easy-slide">
      <i id="prev" class="fa fa-arrow-circle-o-left" aria-hidden="true"></i>
      {thumbnails}
      <li itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
        <a href="{thumbnails:large}" itemprop="contentUrl" data-size="500x500">
          <img src="{thumbnails:thumb}" height="100px" width="100px" itemprop="thumbnail" alt="Image description" />
        </a>
      </li>
      {/thumbnails}
     <i id="next" class="fa fa-arrow-circle-o-right" aria-hidden="true"></i>
  </ul>
</div>

Likely a Content-Type problem. 可能是内容类型问题。 The correct MIME type is not being set when you uploaded the images to S3. 当您将图像上传到S3时,未设置正确的MIME类型。

Just to confirm, check the MIME type being returned: 只是为了确认,请检查返回的MIME类型:

curl -I -v http://www.example.com/image.jpg

Then you will need to set the correct content type in the S3 metadata. 然后,您将需要在S3元数据中设置正确的内容类型。 To update the the metadata on the S3 object, you can copy the object to itself, and specify the content type on the command line. 要更新S3对象上的元数据,可以将对象复制到自身,并在命令行上指定内容类型。

From StackOverflow : How can I change the content-type of an object using aws cli?: StackOverflow :如何使用aws cli更改对象的内容类型?

$ aws s3api copy-object --bucket archive --content-type "image/jpg" \
    --copy-source archive/test/image.jpg--key test/image.jpg \
    --metadata-directive "REPLACE"

To answer your question: 要回答您的问题:

Is there a way to set that for an entire folder or a bucket policy? 有没有办法为整个文件夹或存储桶策略设置该设置?

S3 does not actually have folder/directories. S3实际上没有文件夹/目录。 You need to touch each object via CLI to change its content type. 您需要通过CLI触摸每个对象以更改其内容类型。 See What is the difference between Buckets and Folders in Amazon S3? 请参阅Amazon S3中的存储桶和文件夹有什么区别? . But the command I referenced below will do that operation on an entire bucket. 但是我在下面引用的命令将在整个存储桶中执行该操作。

So you will need to use the S3 CLI to update the content type metadata. 因此,您将需要使用S3 CLI来更新内容类型元数据。 Here is another answer showing the a variety of command line methods, that will change all the content type for all files of a given type (Eg png), recursively: 这是另一个显示各种命令行方法的答案,这些方法将递归地更改给定类型(Eg png)的所有文件的所有内容类型:

aws s3 cp \
      --exclude "*" \
      --include "*.png" \
      --content-type="image/png"  \
      --metadata-directive="REPLACE" \
      --recursive \
      --dryrun \
       s3://mybucket/static/ \
       s3://mybucket/static/

See https://serverfault.com/questions/725562/recursively-changing-the-content-type-for-files-of-a-given-extension-on-amazon-s 参见https://serverfault.com/questions/725562/recursively-changing-the-content-type-for-files-of-a-given-extension-on-amazon-s

When uploading using the API, the default content type is attachment.使用API上传时,默认内容类型为附件。

Sending a request using the content type parameter solves the problem.使用内容类型参数发送请求可以解决问题。 Include the "ContentType" parameter in the header request as below:在 header 请求中包含“ContentType”参数,如下所示:

{
    Bucket: <BUCKET NAME>, 
    Key: "", // pass key
    Body: null, // pass file body,
    ContentType: "image/jpg, image/png, image/jpeg"
}

The URL generated after uploading this way would allow browser access instead of an automatic download.通过这种方式上传后生成的URL将允许浏览器访问而不是自动下载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM