com.google.cloud.storage.Blob.signUrl()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(4.0k)|赞(0)|评价(0)|浏览(220)

本文整理了Java中com.google.cloud.storage.Blob.signUrl()方法的一些代码示例,展示了Blob.signUrl()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Blob.signUrl()方法的具体详情如下:
包路径:com.google.cloud.storage.Blob
类名称:Blob
方法名:signUrl

Blob.signUrl介绍

[英]Generates a signed URL for this blob. If you want to allow access for a fixed amount of time to this blob, you can use this method to generate a URL that is only valid within a certain time period. This is particularly useful if you don't want publicly accessible blobs, but also don't want to require users to explicitly log in. Signing a URL requires a service account signer. If an instance of com.google.auth.ServiceAccountSigner was passed to StorageOptions' builder via setCredentials(Credentials) or the default credentials are being used and the environment variable GOOGLE_APPLICATION_CREDENTIALS is set or your application is running in App Engine, then signUrl will use that credentials to sign the URL. If the credentials passed to StorageOptions do not implement ServiceAccountSigner (this is the case, for instance, for Compute Engine credentials and Google Cloud SDK credentials) then signUrl will throw an IllegalStateExceptionunless an implementation of ServiceAccountSigner is passed using the SignUrlOption#signWith(ServiceAccountSigner) option.

A service account signer is looked for in the following order:

  1. The signer passed with the option SignUrlOption#signWith(ServiceAccountSigner)
  2. The credentials passed to StorageOptions
  3. The default credentials, if no credentials were passed to StorageOptions

Example of creating a signed URL for the blob that is valid for 2 weeks, using the default credentials for signing the URL.

URL signedUrl = blob.signUrl(14, TimeUnit.DAYS);

Example of creating a signed URL for the blob passing the SignUrlOption#signWith(ServiceAccountSigner) option, that will be used to sign the URL.

String keyPath = "/path/to/key.json";

[中]为此blob生成已签名的URL。如果您想允许在固定时间内访问此blob,可以使用此方法生成仅在特定时间段内有效的URL。如果您不希望公开访问blob,但也不希望要求用户显式登录,那么这一点特别有用。对URL进行签名需要服务帐户签名者。如果是com的实例。谷歌。auth。ServiceAccountSigner已通过setCredentials(Credentials)传递给StorageOptions'builder,或者正在使用默认凭据,并且设置了环境变量GOOGLE_APPLICATION_Credentials,或者您的应用程序正在App Engine中运行,则signUrl将使用该凭据对URL进行签名。如果传递给StorageOptions的凭据未实现ServiceAccountSigner(例如,对于计算引擎凭据和Google Cloud SDK凭据,就是这种情况),则signUrl将抛出非法状态Exception,除非使用SignUrlOption#signWith(ServiceAccountSigner)传递ServiceAccountSigner的实现选项
按以下顺序查找服务帐户签名者:
1.签名者通过了选项SignUrlOption#signWith(ServiceAccountSigner)
1.传递给StorageOptions的凭据
1.如果没有向StorageOptions传递凭据,则为默认凭据
使用用于签名URL的默认凭据为blob创建有效期为2周的签名URL的示例。

URL signedUrl = blob.signUrl(14, TimeUnit.DAYS);

为通过SignUrlOption#signWith(ServiceAccountSigner)选项的blob创建签名URL的示例,该选项将用于对URL进行签名。

String keyPath = "/path/to/key.json";

代码示例

代码示例来源:origin: googleapis/google-cloud-java

/**
 * Example of creating a signed URL for the blob that is valid for 2 weeks, using the default
 * credentials for signing the URL.
 */
// [TARGET signUrl(long, TimeUnit, SignUrlOption...)]
public URL signUrl() {
 // [START signUrl]
 URL signedUrl = blob.signUrl(14, TimeUnit.DAYS);
 // [END signUrl]
 return signedUrl;
}

代码示例来源:origin: googleapis/google-cloud-java

/**
 * Example of creating a signed URL for the blob passing the {@link
 * SignUrlOption#signWith(ServiceAccountSigner)} option, that will be used to sign the URL.
 */
// [TARGET signUrl(long, TimeUnit, SignUrlOption...)]
// [VARIABLE "/path/to/key.json"]
public URL signUrlWithSigner(String keyPath) throws IOException {
 // [START signUrlWithSigner]
 URL signedUrl =
   blob.signUrl(
     14,
     TimeUnit.DAYS,
     SignUrlOption.signWith(
       ServiceAccountCredentials.fromStream(new FileInputStream(keyPath))));
 // [END signUrlWithSigner]
 return signedUrl;
}

代码示例来源:origin: googleapis/google-cloud-java

private void run(Storage storage, ServiceAccountCredentials cred, BlobInfo blobInfo) {
 Blob blob = storage.get(blobInfo.getBlobId());
 System.out.printf(
   "Signed URL: %s%n", blob.signUrl(1, TimeUnit.DAYS, SignUrlOption.signWith(cred)));
}

代码示例来源:origin: googleapis/google-cloud-java

@Test
public void testSignUrl() throws Exception {
 initializeExpectedBlob(2);
 URL url = new URL("http://localhost:123/bla");
 expect(storage.getOptions()).andReturn(mockOptions);
 expect(storage.signUrl(expectedBlob, 100, TimeUnit.SECONDS)).andReturn(url);
 replay(storage);
 initializeBlob();
 assertEquals(url, blob.signUrl(100, TimeUnit.SECONDS));
}

相关文章