1. Overview
1.概述
Google Cloud Storage offers online storage tailored to an individual application’s needs based on location, the frequency of access, and cost. Unlike Amazon Web Services, Google Cloud Storage uses a single API for high, medium, and low-frequency access.
谷歌云存储根据位置、访问频率和成本,提供适合个人应用需求的在线存储。与亚马逊网络服务不同,谷歌云存储使用一个单一的API,用于高、中、低频率的访问。
Like most cloud platforms, Google offers a free tier of access; the pricing details are here.
像大多数云平台一样,谷歌提供了一个免费的访问层级;定价详情见这里。。
In this tutorial, we’ll connect to storage, create a bucket, write, read, and update data. While using the API to read and write data, we’ll also use the gsutil cloud storage utility.
在本教程中,我们将连接到存储,创建一个桶,写入、读取和更新数据。在使用API读写数据的同时,我们还将使用gsutil云存储工具。
2. Google Cloud Storage Setup
2.谷歌云存储设置
2.1. Maven Dependency
2.1.Maven的依赖性
We need to add a single dependency to our pom.xml:
我们需要在我们的pom.xml中添加一个依赖项。
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-storage</artifactId>
<version>1.17.0</version>
</dependency>
Maven Central has the latest version of the library.
Maven Central有最新版本的库。
2.2. Create Authentication Key
2.2.创建认证密钥
Before we can connect to Google Cloud, we need to configure authentication. Google Cloud Platform (GCP) applications load a private key and configuration information from a JSON configuration file. We generate this file via the GCP console. Access to the console requires a valid Google Cloud Platform Account.
在连接到谷歌云之前,我们需要配置身份验证。谷歌云平台(GCP)应用程序从一个JSON配置文件中加载私钥和配置信息。我们通过GCP控制台生成这个文件。访问该控制台需要一个有效的谷歌云平台账户。
We create our configuration by:
我们通过以下方式创建我们的配置。
- Going to the Google Cloud Platform Console
- If we haven’t yet defined a GCP project, we click the create button and enter a project name, such as “baeldung-cloud-tutorial“
- Select “new service account” from the drop-down list
- Add a name such as “baeldung-cloud-storage” into the account name field.
- Under “role” select Project, and then Owner in the submenu.
- Select create, and the console downloads a private key file.
The role in step #6 authorizes the account to access project resources. For the sake of simplicity, we gave this account complete access to all project resources.
步骤#6中的角色授权该账户访问项目资源。为了简单起见,我们让这个账户完全访问所有项目资源。
For a production environment, we would define a role that corresponds to the access the application needs.
对于生产环境,我们将定义一个与应用程序所需的访问权限相对应的角色。
2.3. Install the Authentication Key
2.3.安装认证密钥
Next, we copy the file downloaded from GCP console to a convenient location and point the GOOGLE_APPLICATION_CREDENTIALS environment variable at it. This is the easiest way to load the credentials, although we’ll look at another possibility below.
接下来,我们将从GCP控制台下载的文件复制到一个方便的位置,并将GOOGLE_APPLICATION_CREDENTIALS环境变量指向它。这是最简单的加载证书的方法,尽管我们将在下面看另一种可能性。
For Linux or Mac:
适用于Linux或Mac。
export GOOGLE_APPLICATION_CREDENTIALS="/path/to/file"
For Windows:
对于Windows。
set GOOGLE_APPLICATION_CREDENTIALS="C:\path\to\file"
2.4. Install Cloud Tools
2.4.安装云工具
Google provides several tools for managing their cloud platform. We’re going to use gsutil during this tutorial to read and write data alongside the API.
谷歌提供了几个工具来管理他们的云平台。在本教程中,我们将使用gsutil来读取和写入API旁边的数据。
We can do this in two easy steps:
我们可以通过两个简单的步骤做到这一点。
- Install the Cloud SDK from the instructions here for our platform.
- Follow the Quickstart for our platform here. In step 4 of Initialize the SDK, we select the project name in step 4 of section 2.2 above (“baeldung-cloud-storage” or whichever name you used).
gsutil is now installed and configured to read data from our cloud project.
gsutil现在已经安装并配置好,可以从我们的云项目中读取数据。
3. Connecting to Storage and Creating a Bucket
3.连接到Storage并创建Bucket
3.1. Connect to Storage
3.1.连接到存储
Before we can use Google Cloud storage, we have to create a service object. If we’ve already set up the GOOGLE_APPLICATION_CREDENTIALS environment variable, we can use the default instance:
在我们使用谷歌云存储之前,我们必须创建一个服务对象。如果我们已经设置了GOOGLE_APPLICATION_CREDENTIALS环境变量,我们可以使用默认实例。
Storage storage = StorageOptions.getDefaultInstance().getService();
If we don’t want to use the environment variable, we have to create a Credentials instance and pass it to Storage with the project name:
如果我们不想使用环境变量,我们必须创建一个Credentials 实例,并将其与Storage的项目名称一起传递给Storage。
Credentials credentials = GoogleCredentials
.fromStream(new FileInputStream("path/to/file"));
Storage storage = StorageOptions.newBuilder().setCredentials(credentials)
.setProjectId("baeldung-cloud-tutorial").build().getService();
3.2. Creating a Bucket
3.2.创建一个Bucket
Now that we’re connected and authenticated, we can create a bucket. Buckets are containers that hold objects. They can be used to organize and control data access.
现在,我们已经连接并通过了认证,我们可以创建一个桶。水桶是容纳对象的容器。它们可以用来组织和控制数据访问。
There is no limit to the number of objects in a bucket. GCP limits the numbers of operations on buckets and encourages application designers to emphasize operations on objects rather than on buckets.
对于一个桶中的对象的数量没有限制。GCP 限制了对桶的操作数量,并且鼓励应用程序设计者强调对对象而不是对桶的操作。。
Creating a bucket requires a BucketInfo:
创建一个桶需要一个BucketInfo:。
Bucket bucket = storage.create(BucketInfo.of("baeldung-bucket"));
For this simple example, we a bucket name and accept the default properties. Bucket names must be globally unique. If we choose a name that is already used, create() will fail.
在这个简单的例子中,我们有一个桶的名字,并接受默认的属性。桶的名字必须是全球唯一的。如果我们选择一个已经被使用的名字,create()将会失败。
3.3. Examing a Bucket With gsutil
3.3.使用gsutil检查Bucket
Since we have a bucket now, we can take a examine it with gsutil.
由于我们现在有一个水桶,我们可以用gsutil.来检查它。
Let’s open a command prompt and take a look:
让我们打开一个命令提示符,看一看。
$ gsutil ls -L -b gs://baeldung-1-bucket/
gs://baeldung-1-bucket/ :
Storage class: STANDARD
Location constraint: US
Versioning enabled: None
Logging configuration: None
Website configuration: None
CORS configuration: None
Lifecycle configuration: None
Requester Pays enabled: None
Labels: None
Time created: Sun, 11 Feb 2018 21:09:15 GMT
Time updated: Sun, 11 Feb 2018 21:09:15 GMT
Metageneration: 1
ACL:
[
{
"entity": "project-owners-385323156907",
"projectTeam": {
"projectNumber": "385323156907",
"team": "owners"
},
"role": "OWNER"
},
...
]
Default ACL:
[
{
"entity": "project-owners-385323156907",
"projectTeam": {
"projectNumber": "385323156907",
"team": "owners"
},
"role": "OWNER"
},
...
]
gsutil looks a lot like shell commands, and anyone familiar with the Unix command line should feel very comfortable here. Notice we passed in the path to our bucket as a URL: gs://baeldung-1-bucket/, along with a few other options.
gsutil看起来很像shell命令,任何熟悉Unix命令行的人在这里都应该感到非常舒服。注意,我们把我们的桶的路径作为一个URL传入。gs://baeldung-1-bucket/,以及其他一些选项。
The ls option produces a listing or objects or buckets, and the -L option indicated that we want a detailed listing – so we received details about the bucket including the creation times and access controls.
ls选项产生一个列表或objects或buckets,而-L选项表示我们想要一个详细的列表–所以我们收到了关于bucket的细节,包括创建时间和访问控制。
Let’s add some data to our bucket!
让我们向我们的水桶添加一些数据吧!
4. Reading, Writing and Updating Data
4.读、写和更新数据
In Google Cloud Storage, objects are stored in Blobs; Blob names can contain any Unicode character, limited to 1024 characters.
在谷歌云存储中,对象被存储在Blobs中;Blob名称可以包含任何Unicode字符,限制在1024个字符。
4.1. Writing Data
4.1.写入数据
Let’s save a String to our bucket:
让我们保存一个String到我们的桶中。
String value = "Hello, World!";
byte[] bytes = value.getBytes(UTF_8);
Blob blob = bucket.create("my-first-blob", bytes);
As you can see, objects are simply arrays of bytes in the bucket, so we store a String by simply operating with its raw bytes.
正如你所看到的,对象只是桶中字节的数组,所以我们通过简单地操作其原始字节来存储一个字符串。
4.2. Reading Data with gsutil
4.2.用gsutil读取数据
Now that we have a bucket with an object in it, let’s take a look at gsutil.
现在我们有了一个装有对象的桶,让我们看看gsutil.。
Let’s start by listing the contents of our bucket:
让我们首先列出我们的水桶的内容。
$ gsutil ls gs://baeldung-1-bucket/
gs://baeldung-1-bucket/my-first-blob
We passed gsutil the ls option again but omitted -b and -L, so we asked for a brief listing of objects. We receive a list of URIs for each object, which is one in our case.
我们再次向gsutil传递了ls选项,但省略了-b和-L,所以我们要求提供一个对象的简要列表。我们收到每个对象的URI列表,在我们的例子中是一个。
Let’s examine the object:
让我们研究一下这个对象。
$ gsutil cat gs://baeldung-1-bucket/my-first-blob
Hello World!
Cat concatenates the contents of the object to standard output. We see the String we wrote to the Blob.
Cat将对象的内容连接到标准输出。我们看到了我们写给Blob的String。
4.3. Reading Data
4.3.读取数据
Blobs are assigned a BlobId upon creation.
Blobs在创建时被分配了一个BlobId。
The easiest way to retrieve a Blob is with the BlobId:
检索Blob的最简单方法是使用BlobId。
Blob blob = storage.get(blobId);
String value = new String(blob.getContent());
We pass the id to Storage and get the Blob in return, and getContent() returns the bytes.
我们将id传递给Storage,并得到Blob的回报,getContent()返回字节。
If we don’t have the BlobId, we can search the Bucket by name:
如果我们没有BlobId,我们可以通过名称搜索Bucket。
Page<Blob> blobs = bucket.list();
for (Blob blob: blobs.getValues()) {
if (name.equals(blob.getName())) {
return new String(blob.getContent());
}
}
4.4. Updating Data
4.4.更新数据
We can update a Blob by retrieving it and then accessing its WriteableByteChannel:
我们可以通过检索一个Blob,然后访问其WriteableByteChannel来更新该Blob。
String newString = "Bye now!";
Blob blob = storage.get(blobId);
WritableByteChannel channel = blob.writer();
channel.write(ByteBuffer.wrap(newString.getBytes(UTF_8)));
channel.close();
Let’s examine the updated object:
让我们检查一下更新后的对象。
$ gsutil cat gs://baeldung-1-bucket/my-first-blob
Bye now!
4.5. Save an Object to File, Then Delete
4.5.将一个对象保存到文件中,然后删除
Let’s save the updated the object to a file:
让我们把更新后的对象保存到一个文件中。
$ gsutil copy gs://baeldung-1-bucket/my-first-blob my-first-blob
Copying gs://baeldung-1-bucket/my-first-blob...
/ [1 files][ 9.0 B/ 9.0 B]
Operation completed over 1 objects/9.0 B.
Grovers-Mill:~ egoebelbecker$ cat my-first-blob
Bye now!
As expected, the copy option copies the object to the filename specified on the command line.
正如预期的那样,copy选项将对象复制到命令行上指定的文件名。
gsutil can copy any object from Google Cloud Storage to the local file system, assuming there is enough space to store it.
gsutil可以将任何对象从谷歌云存储复制到本地文件系统,前提是有足够的空间来存储它。
We’ll finish by cleaning up:
我们将以清理结束。
$ gsutil rm gs://baeldung-1-bucket/my-first-blob
Removing gs://baeldung-1-bucket/my-first-blob...
/ [1 objects]
Operation completed over 1 objects.
$ gsutil ls gs://baeldung-1-bucket/
$
rm (del works too) deletes the specified object
rm(del也可以)删除指定的对象。
5. Conclusion
5.结论
In this brief tutorial, we created credentials for Google Cloud Storage and connected to the infrastructure. We created a bucket, wrote data, and then read and modified it. As we’re working with the API, we also used gsutil to examine cloud storage as we created and read data.
在这个简短的教程中,我们创建了谷歌云存储的凭证并连接到基础设施。我们创建了一个桶,写入数据,然后读取和修改它。由于我们正在使用API,我们还使用gsutil来检查云存储,因为我们创建和读取数据。
We also discussed how to use buckets and write and modify data efficiently.
我们还讨论了如何使用桶以及有效地写入和修改数据。
Code samples, as always, can be found over on GitHub.
像往常一样,可以在GitHub上找到代码样本。