简体   繁体   English

我们如何从亚马逊 s3 存储桶中获取查看 url 和下载 url?

[英]How can we get the view url and download url from amazon s3 bucket?

Uploaded file with two methods.使用两种方法上传文件。 Method 1 .方法一 file is MultipartFile type文件是 MultipartFile 类型

InputStream input =file.getInputStream();
   ObjectMetadata metadata=new ObjectMetadata();
   metadata.setContentLength(file.getSize());
   amazon.putObject(bucketName, fileName,input,metadata);

Method 2 .方法二 fileObj is of file type fileObj 是文件类型

amazon.putObject(bucketName, fileName,fileObj); 

Output:- String url = amazon.getUrl(bucketName, fileName).toExternalForm();输出:- String url = amazon.getUrl(bucketName, fileName).toExternalForm();

Method 1: - If I hit the url, it gets downloaded方法 1: - 如果我点击 url,它会被下载

Method 2: - If I hit the url, it can view from the browser.方法 2: - 如果我点击 url,它可以从浏览器中查看。

How do I achieve both view and download from any one method?如何通过任何一种方法实现查看和下载? Also which is more convenient and effective.也更方便有效。 Thanks in advance.提前致谢。

If an object is uploaded to Amazon S3 with Content-Type metadata, the browser can understand its format and display it (if it knows how).如果一个对象使用Content-Type元数据上传到 Amazon S3,浏览器可以理解它的格式并显示它(如果它知道如何)。

It would appear that this happens automatically with your Method 2, but boto3 is unable to detect the Content-Type when using Method 1. You would need to add this metadata yourself to produce the same result as Method 2.看起来这会在您的方法 2 中自动发生,但是在使用方法 1 时 boto3 无法检测到 Content-Type。您需要自己添加此元数据以产生与方法 2 相同的结果。

To control what is viewed and downloaded, do it in your HTML:要控制查看和下载的内容,请在 HTML 中执行此操作:

<a href="...">View object</a>
<a href="..." download>Download object</a>

暂无
暂无

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

相关问题 我们如何从S3亚马逊服务器打开文件URL中获取Java中的File()类型对象? - How can we get File() type object in Java from S3 amazon server open file URL? 如何在 Amazon s3 Bucket 中压缩文件并获取其 URL - How to zip files in Amazon s3 Bucket and get its URL 如何获取刚刚放入Amazon S3存储桶中的对象的URL? - How do I get the URL of an object I've just put in my Amazon S3 bucket? 如何在当前日期的1年内设置Amazon S3存储桶预签名URL到期时间 - How to set Amazon S3 bucket presigned URL expiration time in 1 year from current date 我们如何使用 Java SDK 从 S3 存储桶下载没有文件夹的多个文件 - How can we download multiple files without folder from S3 bucket using Java SDK 如何将图像从Amazon S3下载并保存到图像视图 - How to download and save an image from Amazon S3 to an Image View 如何从Java中的bucket.s3.amazonaws.com/key更改s3.amazonaws.com/bucket/key之类的Amazon S3文件URL? - How to Change Amazon S3 file URL Like s3.amazonaws.com/bucket/key from bucket.s3.amazonaws.com/key in Java? 当密钥使用文件夹结构定义时,我们如何从 AWS s3 存储桶中获取 Object? Java - How can we get the Object from the AWS s3 bucket when the key is defined with folder structure? Java 如何使用Java从Amazon S3存储桶中使用其名称下载垂直文件 - how to download a perticular file using it's name from amazon s3 bucket using java Amazon S3上传文件并获取URL - Amazon S3 upload file and get URL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM