我写一个函数来创建缩略图,但是缩略图没有公开链接,即使原始文件具有的权限User
- allUsers
-Reader
和公众联系https://storage.googleapis.com/{buckets}/{filename}
的所有用户访问的文件。
如何将此权限添加到缩略图中。
public static void thumbnailImage(String filename, int width, int height) throws IOException{
GcsService gcsService = GcsServiceFactory.createGcsService(new RetryParams.Builder()
.initialRetryDelayMillis(10)
.retryMaxAttempts(10)
.totalRetryPeriodMillis(15000)
.build());
AppIdentityService appIdentity = AppIdentityServiceFactory.getAppIdentityService();
ImagesService imagesService = ImagesServiceFactory.getImagesService();
// Make an image from a Cloud Storage object, and transform it.
BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
BlobKey blobKey = blobstoreService.createGsBlobKey("/gs/" + appIdentity.getDefaultGcsBucketName() +"/"+ filename);
Image thumb = null;
try{
Image blobImage = ImagesServiceFactory.makeImageFromBlob(blobKey);
Transform resize = ImagesServiceFactory.makeResize(width, height);
thumb = imagesService.applyTransform(resize, blobImage);
}catch(Exception e){
e.printStackTrace();
}
String extension = Files.getFileExtension(filename);
filename = stripExtension(filename);
filename = String.format("%1$s_%2$s.%3$s", filename, thumbnail, extension);
if(thumb!=null)
// Write the transformed image back to a Cloud Storage object.
gcsService.createOrReplace(
new GcsFilename(appIdentity.getDefaultGcsBucketName(), filename),
new GcsFileOptions.Builder().mimeType("image/jpeg").build(),
ByteBuffer.wrap(thumb.getImageData()));
}
您可以使用GcsFileOption参数“ acl”使此设置对其他人可见。因此,请在此末尾更改该位:
new GcsFileOptions.Builder().mimeType("image/jpeg").acl("public-read").build(),
公开链接将具有相同的storage.googleapis.com/bucket/object模式,但object是缩略图的名称。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句