使用Java将对象上传到GCP后,上传的对象的公共URL是下载链接 [英] Uploaded object Public URL is download link after uploading the object to GCP using java
问题描述
我已使用以下Java库代码将对象成功上传到了Google云存储中
I have successfully uploaded the object to the google cloud storage using the Java library code as below
public static void uploadObject(
String projectId, String bucketName, String objectName, String filePath) throws IOException {
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
BlobId blobId = BlobId.of(bucketName, objectName);
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();
storage.create(blobInfo, Files.readAllBytes(Paths.get(filePath)));
System.out.println(
"File " + filePath + " uploaded to bucket " + bucketName + " as " + objectName);
}
如果我浏览公共URL
If I browse the public URL
如果我浏览URL https://storage.googleapis.com/fetebird-product/bird.jpg 该文件是可下载的链接.如何使它只是一个简单的浏览文件
If I browse the URL https://storage.googleapis.com/fetebird-product/bird.jpg the file is the downloadable link. How can I make it just a simple browsing file
我想念什么?
推荐答案
这是Googling/StackOverflow的回答,其中指出:
This is an answer from Googling/StackOverflow, which states:
向浏览器指示应在浏览器,HTTP响应应包含以下标头:
To indicate to the browser that the file should be viewed in the browser, the HTTP response should include these headers:
内容类型:应用程序/pdf内容处置:内联;filename ="filename.pdf"
要下载文件而不是查看文件:
To have the file downloaded rather than viewed:
内容类型:应用程序/pdf内容处置:附件;filename ="filename.pdf"
如果文件名包含以下内容,则必须在文件名周围加上引号特殊字符,例如filename [1] .pdf,否则可能会中断浏览器处理响应的能力.
The quotes around the filename are required if the filename contains special characters such as filename[1].pdf which may otherwise break the browser's ability to handle the response.
如何设置HTTP响应标头取决于您的HTTP服务器(或者,如果要从服务器端代码生成PDF响应,请执行以下操作:您的服务器端编程语言.
How you set the HTTP response headers will depend on your HTTP server (or, if you are generating the PDF response from server-side code: your server-side programming language).
因此,按照您的逻辑,您需要执行以下操作:
Therefore, in your logic you need to do:
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("image/jpeg").setContentDisposition(String.format("inline; filename=\"%s\"", yourPictureFileName)).build();
这篇关于使用Java将对象上传到GCP后,上传的对象的公共URL是下载链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!