本文整理汇总了Java中com.google.api.services.storage.StorageScopes类的典型用法代码示例。如果您正苦于以下问题:Java StorageScopes类的具体用法?Java StorageScopes怎么用?Java StorageScopes使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
StorageScopes类属于com.google.api.services.storage包,在下文中一共展示了StorageScopes类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: loadCredentials
import com.google.api.services.storage.StorageScopes; //导入依赖的package包/类
/**
* HTTP request initializer that loads credentials from the service account file
* and manages authentication for HTTP requests
*/
private GoogleCredential loadCredentials(String serviceAccount) throws IOException {
if (serviceAccount == null) {
throw new ElasticsearchException("Cannot load Google Cloud Storage service account file from a null path");
}
Path account = environment.configFile().resolve(serviceAccount);
if (Files.exists(account) == false) {
throw new ElasticsearchException("Unable to find service account file [" + serviceAccount
+ "] defined for repository");
}
try (InputStream is = Files.newInputStream(account)) {
GoogleCredential credential = GoogleCredential.fromStream(is);
if (credential.createScopedRequired()) {
credential = credential.createScoped(Collections.singleton(StorageScopes.DEVSTORAGE_FULL_CONTROL));
}
return credential;
}
}
示例2: loadStorageCredential
import com.google.api.services.storage.StorageScopes; //导入依赖的package包/类
private static GoogleCredential loadStorageCredential(HttpTransport transport, JsonFactory factory, String jsonPath) throws IOException {
GoogleCredential credential;
if (!jsonPath.isEmpty()) {
FileInputStream stream = new FileInputStream(jsonPath);
credential = GoogleCredential.fromStream(stream, transport, factory);
log.info("Loaded storage credentials from " + jsonPath);
} else {
log.info("Using storage default application credentials.");
credential = GoogleCredential.getApplicationDefault();
}
if (credential.createScopedRequired()) {
credential = credential.createScoped(StorageScopes.all());
}
return credential;
}
示例3: getService
import com.google.api.services.storage.StorageScopes; //导入依赖的package包/类
/**
* Method to create the service or get the service if is already created.
*
* @author <a href="mailto:[email protected]"> João Felipe de Medeiros Moreira </a>
* @since 13/10/2015
*
* @return the Storage service already created.
*
* @throws IOException in case a IO problem.
* @throws GeneralSecurityException in case a security problem.
*/
private static Storage getService() throws IOException, GeneralSecurityException {
logger.finest("###### Getting the storage service");
if (null == storageService) {
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
GoogleCredential credential = GoogleCredential.getApplicationDefault();
// Depending on the environment that provides the default credentials (e.g. Compute Engine,
// App Engine), the credentials may require us to specify the scopes we need explicitly.
// Check for this case, and inject the Cloud Storage scope if required.
if (credential.createScopedRequired()) {
credential = credential.createScoped(StorageScopes.all());
}
storageService = new Storage.Builder(httpTransport, JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME).build();
}
return storageService;
}
示例4: getServiceAccountCredential
import com.google.api.services.storage.StorageScopes; //导入依赖的package包/类
/**
* @see https://developers.google.com/accounts/docs/OAuth2ServiceAccount#authorizingrequests
*/
private GoogleCredential getServiceAccountCredential() throws IOException, GeneralSecurityException
{
// @see https://cloud.google.com/compute/docs/api/how-tos/authorization
// @see https://developers.google.com/resources/api-libraries/documentation/storage/v1/java/latest/com/google/api/services/storage/STORAGE_SCOPE.html
// @see https://developers.google.com/resources/api-libraries/documentation/bigquery/v2/java/latest/com/google/api/services/bigquery/BigqueryScopes.html
return new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId(serviceAccountEmail.orNull())
.setServiceAccountScopes(
ImmutableList.of(
StorageScopes.DEVSTORAGE_READ_WRITE
)
)
.setServiceAccountPrivateKeyFromP12File(new File(p12KeyFilePath.get()))
.build();
}
示例5: buildService
import com.google.api.services.storage.StorageScopes; //导入依赖的package包/类
private static Storage buildService() throws IOException, GeneralSecurityException {
HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory jsonFactory = new JacksonFactory();
GoogleCredential credential = GoogleCredential.getApplicationDefault(transport, jsonFactory);
// Depending on the environment that provides the default credentials (for
// example: Compute Engine, App Engine), the credentials may require us to
// specify the scopes we need explicitly. Check for this case, and inject
// the Cloud Storage scope if required.
if (credential.createScopedRequired()) {
Collection<String> scopes = StorageScopes.all();
credential = credential.createScoped(scopes);
}
return new Storage.Builder(transport, jsonFactory, credential)
.setApplicationName("GCS Samples")
.build();
}
示例6: getService
import com.google.api.services.storage.StorageScopes; //导入依赖的package包/类
private static Storage getService(String credentialsPath, int connectTimeoutMs, int readTimeoutMs) throws Exception {
if (mStorageService == null) {
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
GoogleCredential credential;
try {
// Lookup if configured path from the properties; otherwise fallback to Google Application default
if (credentialsPath != null && !credentialsPath.isEmpty()) {
credential = GoogleCredential
.fromStream(new FileInputStream(credentialsPath), httpTransport, JSON_FACTORY)
.createScoped(Collections.singleton(StorageScopes.CLOUD_PLATFORM));
} else {
credential = GoogleCredential.getApplicationDefault(httpTransport, JSON_FACTORY);
}
} catch (IOException e) {
throw new RuntimeException("Failed to load Google credentials : " + credentialsPath, e);
}
mStorageService = new Storage.Builder(httpTransport, JSON_FACTORY,
setHttpBackoffTimeout(credential, connectTimeoutMs, readTimeoutMs))
.setApplicationName("com.pinterest.secor")
.build();
}
return mStorageService;
}
示例7: constructStorageApiStub
import com.google.api.services.storage.StorageScopes; //导入依赖的package包/类
private static Storage constructStorageApiStub() throws GeneralSecurityException, IOException {
JsonFactory jsonFactory = new JacksonFactory();
HttpTransport transport;
transport = GoogleNetHttpTransport.newTrustedTransport();
GoogleCredential credential = GoogleCredential.getApplicationDefault(transport, jsonFactory);
if (credential.createScopedRequired()) {
Collection<String> scopes = StorageScopes.all();
credential = credential.createScoped(scopes);
}
return new Storage.Builder(transport, jsonFactory, credential)
.setApplicationName("GCS Samples")
.build();
}
示例8: buildService
import com.google.api.services.storage.StorageScopes; //导入依赖的package包/类
private static Storage buildService() throws IOException, GeneralSecurityException {
HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory jsonFactory = new JacksonFactory();
GoogleCredential credential = GoogleCredential.getApplicationDefault(transport, jsonFactory);
if (credential.createScopedRequired()) {
Collection<String> bigqueryScopes = StorageScopes.all();
credential = credential.createScoped(bigqueryScopes);
}
return new Storage.Builder(transport, jsonFactory, credential)
.setApplicationName("MCSFS")
.build();
}
示例9: loadCredential
import com.google.api.services.storage.StorageScopes; //导入依赖的package包/类
private GoogleCredential loadCredential(HttpTransport transport, JsonFactory factory, String jsonPath) throws IOException {
GoogleCredential credential;
if (!jsonPath.isEmpty()) {
FileInputStream stream = new FileInputStream(jsonPath);
credential = GoogleCredential.fromStream(stream, transport, factory)
.createScoped(Collections.singleton(StorageScopes.DEVSTORAGE_FULL_CONTROL));
log.info("Loaded credentials from " + jsonPath);
} else {
log.info("Using default application credentials.");
credential = GoogleCredential.getApplicationDefault();
}
return credential;
}
示例10: readStorageFile
import com.google.api.services.storage.StorageScopes; //导入依赖的package包/类
public InputStream readStorageFile(String file) throws IOException, GeneralSecurityException {
httpTransport = GoogleNetHttpTransport.newTrustedTransport();
Set<String> scopes = new HashSet<String>();
scopes.add(StorageScopes.DEVSTORAGE_FULL_CONTROL);
scopes.add(StorageScopes.DEVSTORAGE_READ_ONLY);
scopes.add(StorageScopes.DEVSTORAGE_READ_WRITE);
GoogleCredential credential = null;
try {
credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(Constants.GCP_ACCOUNT_ID)
.setServiceAccountScopes(scopes)
.setServiceAccountPrivateKeyFromP12File(new File(Constants.GCP_ACCOUNT_KEY))
.build();
} catch (GeneralSecurityException e) {
e.printStackTrace();
}
Storage storage = new Storage.Builder(httpTransport, JSON_FACTORY, credential)
.setApplicationName(Constants.APPLICATION_NAME).build();
Storage.Objects.Get get = storage.objects().get(Constants.GCP_STORAGE_BUCKET, file);
// OutputStream stream = new OutputStream(file);
InputStream is = get.executeMediaAsInputStream();
return is;
}
示例11: createClient
import com.google.api.services.storage.StorageScopes; //导入依赖的package包/类
private Storage createClient() throws IOException, GeneralSecurityException {
GoogleCredential credential = GoogleCredential.fromStream(
new ByteArrayInputStream(settings.serviceAccount.getBytes(StandardCharsets.UTF_8)))
.createScoped(Collections.singleton(StorageScopes.CLOUD_PLATFORM));
NetHttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
return new Storage.Builder(
httpTransport, JSON_FACTORY, null).setApplicationName(APPLICATION_NAME)
.setHttpRequestInitializer(credential).build();
}
示例12: authorize
import com.google.api.services.storage.StorageScopes; //导入依赖的package包/类
/**
* Authorizes the installed application to access user's protected data.
*/
public static Credential authorize() throws IOException {
// Load client secrets.
final byte[] bytes = Files.toByteArray(new File(clientSecretFile));
final GoogleClientSecrets clientSecrets = GoogleClientSecrets
.load(JSON_FACTORY, new InputStreamReader(new ByteArrayInputStream(bytes)));
if (clientSecrets.getDetails().getClientId() == null
|| clientSecrets.getDetails().getClientSecret() == null) {
throw new IllegalStateException("client_secrets not well formed.");
}
// Set up authorization code flow.
// Ask for only the permissions you need. Asking for more permissions will
// reduce the number of users who finish the process for giving you access
// to their accounts. It will also increase the amount of effort you will
// have to spend explaining to users what you are doing with their data.
// Here we are listing all of the available scopes. You should remove scopes
// that you are not actually using.
final Set<String> scopes = new HashSet<String>();
scopes.add(StorageScopes.DEVSTORAGE_FULL_CONTROL);
scopes.add(StorageScopes.DEVSTORAGE_READ_ONLY);
scopes.add(StorageScopes.DEVSTORAGE_READ_WRITE);
final GoogleAuthorizationCodeFlow flow =
new GoogleAuthorizationCodeFlow.Builder(httpTransport, JSON_FACTORY, clientSecrets,
scopes).setDataStoreFactory(dataStoreFactory).build();
// Authorize.
final VerificationCodeReceiver receiver =
AUTH_LOCAL_WEBSERVER ? new LocalServerReceiver() : new GooglePromptReceiver();
return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
}
示例13: getServiceAccountCredentialFromJsonFile
import com.google.api.services.storage.StorageScopes; //导入依赖的package包/类
private GoogleCredential getServiceAccountCredentialFromJsonFile() throws IOException
{
FileInputStream stream = new FileInputStream(jsonKeyFilePath.get());
return GoogleCredential.fromStream(stream, httpTransport, jsonFactory)
.createScoped(Collections.singleton(StorageScopes.DEVSTORAGE_READ_WRITE));
}
示例14: gcsClient
import com.google.api.services.storage.StorageScopes; //导入依赖的package包/类
private Storage gcsClient(GoogleCredential credential)
{
if (credential.createScopedRequired()) {
credential = credential.createScoped(StorageScopes.all());
}
return new Storage.Builder(transport, jsonFactory, credential)
.setApplicationName("digdag-test")
.build();
}
示例15: createCredentials
import com.google.api.services.storage.StorageScopes; //导入依赖的package包/类
private Credential createCredentials(Config configuration) throws IOException {
final String credentialsPath = GcsContext.getCredentialsPath(configuration);
if (!Strings.isNullOrEmpty(credentialsPath)) {
LOG.info("Using credentials from file: " + credentialsPath);
return GoogleCredential
.fromStream(new FileInputStream(credentialsPath))
.createScoped(StorageScopes.all());
}
// if a credentials path is not provided try using the application default one.
LOG.info("Using default application credentials");
return GoogleCredential.getApplicationDefault().createScoped(StorageScopes.all());
}