App Engine & Cloud Storage 如何以编程方式使用 public link 权限保存文件
App Engine & Cloud Storage How to save file with the permission with a public link programmatically
我写了一个创建缩略图的函数,但是缩略图没有public link,即使原始文件有权限User
-allUsers
-Reader
和 public link 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(),
public link 与 storage.googleapis.com/bucket/object 模式相同,只是对象是缩略图的名称。
我写了一个创建缩略图的函数,但是缩略图没有public link,即使原始文件有权限User
-allUsers
-Reader
和 public link 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(),
public link 与 storage.googleapis.com/bucket/object 模式相同,只是对象是缩略图的名称。