-
Notifications
You must be signed in to change notification settings - Fork 285
/
Copy pathStorageClient.java
127 lines (112 loc) · 5.06 KB
/
StorageClient.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.firebase.cloud;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.cloud.firestore.Firestore;
import com.google.cloud.firestore.FirestoreOptions;
import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Strings;
import com.google.firebase.FirebaseApp;
import com.google.firebase.ImplFirebaseTrampolines;
import com.google.firebase.internal.FirebaseService;
import com.google.firebase.internal.NonNull;
/**
* StorageClient provides access to Google Cloud Storage APIs. You can specify a default cloud
* storage bucket via {@link com.google.firebase.FirebaseOptions}, and then get a reference to this
* default bucket by calling {@link StorageClient#bucket()}. Or you can get a reference to a
* specific bucket at any time by calling {@link StorageClient#bucket(String)}.
*
* <p>This class requires Google Cloud Storage libraries for Java. Make sure the artifact
* google-cloud-storage is in the classpath along with its transitive dependencies.
*/
public class StorageClient {
private final FirebaseApp app;
private final Storage storage;
@VisibleForTesting
StorageClient(FirebaseApp app, Storage storage) {
this.app = checkNotNull(app, "FirebaseApp must not be null");
this.storage = checkNotNull(storage, "Storage must not be null");
}
public static StorageClient getInstance() {
return getInstance(FirebaseApp.getInstance());
}
public static synchronized StorageClient getInstance(FirebaseApp app) {
StorageClientService service = ImplFirebaseTrampolines.getService(app, SERVICE_ID,
StorageClientService.class);
if (service == null) {
StorageOptions userOptions = app.getOptions().getStorageOptions();
StorageOptions.Builder builder = userOptions != null ? userOptions.toBuilder() :
StorageOptions.newBuilder();
Storage storage = builder
.setCredentials(ImplFirebaseTrampolines.getCredentials(app))
.setProjectId(ImplFirebaseTrampolines.getProjectId(app))
.build()
.getService();
StorageClient client = new StorageClient(app, storage);
service = ImplFirebaseTrampolines.addService(app, new StorageClientService(client));
}
return service.getInstance();
}
@NonNull
public static Storage getStorage(FirebaseApp app) {
return getInstance(app).storage;
}
@NonNull
public static Storage getStorage() {
return getStorage(FirebaseApp.getInstance());
}
/**
* Returns the default cloud storage bucket associated with the current app. This is the bucket
* configured via {@link com.google.firebase.FirebaseOptions} when initializing the app. If
* no bucket was configured via options, this method throws an exception.
*
* @return a cloud storage <a href="https://googlecloudplatform.github.io/google-cloud-java/google-cloud-clients/apidocs/com/google/cloud/storage/Bucket.html">{@code Bucket}</a>
* instance.
* @throws IllegalArgumentException If no bucket is configured via <code>FirebaseOptions</code>,
* or if the bucket does not exist.
*/
public Bucket bucket() {
return bucket(app.getOptions().getStorageBucket());
}
/**
* Returns a cloud storage Bucket instance for the specified bucket name.
*
* @param name a non-null, non-empty bucket name.
* @return a cloud storage <a href="https://googlecloudplatform.github.io/google-cloud-java/latest/google-cloud-clients/com/google/cloud/storage/Bucket.html">{@code Bucket}</a>
* instance.
* @throws IllegalArgumentException If the bucket name is null, empty, or if the specified
* bucket does not exist.
*/
public Bucket bucket(String name) {
checkArgument(!Strings.isNullOrEmpty(name),
"Bucket name not specified. Specify the bucket name via the storageBucket "
+ "option when initializing the app, or specify the bucket name explicitly when "
+ "calling the getBucket() method.");
Bucket bucket = storage.get(name);
checkArgument(bucket != null, "Bucket " + name + " does not exist.");
return bucket;
}
private static final String SERVICE_ID = StorageClient.class.getName();
private static class StorageClientService extends FirebaseService<StorageClient> {
StorageClientService(StorageClient client) {
super(SERVICE_ID, client);
}
}
}