diff --git a/gcloud-java-contrib/gcloud-java-nio/src/main/java/com/google/cloud/storage/contrib/nio/package-info.java b/gcloud-java-contrib/gcloud-java-nio/src/main/java/com/google/cloud/storage/contrib/nio/package-info.java index bee99002b82f..1376ca25d02e 100644 --- a/gcloud-java-contrib/gcloud-java-nio/src/main/java/com/google/cloud/storage/contrib/nio/package-info.java +++ b/gcloud-java-contrib/gcloud-java-nio/src/main/java/com/google/cloud/storage/contrib/nio/package-info.java @@ -22,43 +22,55 @@ * *
{@code - * + * The simplest way to get started is with {@code Paths} and {@code Files}: + *+ * + *{@code * Path path = Paths.get(URI.create("gs://bucket/lolcat.csv")); - * List+ * Listlines = Files.readAllLines(path, StandardCharsets.UTF_8);} lines = Files.readAllLines(path, StandardCharsets.UTF_8); + * }
For the complete source code see + * + * ReadAllLines.java. * *
If you want to configure the bucket per-environment, it might make more sense to use the * {@code FileSystem} API: - *
- * class Foo { - * static String bucket = System.getProperty(...); - * static FileSystem fs = FileSystems.getFileSystem(URI.create("gs://" + bucket)); - * void bar() { - * byte[] data = "hello world".getBytes(StandardCharsets.UTF_8); - * Path path = fs.getPath("/object"); - * Files.write(path, data); - * data = Files.readBytes(path); - * } - * void doodle() { - * Path path = fs.getPath("/path/to/doodle.csv"); - * List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8); - * } - * }- * - *
You can also use InputStream and OutputStream for streaming: + *
{@code + * FileSystem fs = FileSystems.getFileSystem(URI.create("gs://bucket")); + * byte[] data = "hello world".getBytes(StandardCharsets.UTF_8); + * Path path = fs.getPath("/object"); + * Files.write(path, data); + * List+ * + *lines = Files.readAllLines(path, StandardCharsets.UTF_8); + * }
For the complete source code see + * + * GetFileSystem.java. + * + *
You can also use {@code InputStream} and {@code OutputStream} for streaming: *
* Path path = Paths.get(URI.create("gs://bucket/lolcat.csv")); - * try (InputStream input = Files.openInputStream(path)) { - * // ... - * }+ * try (InputStream input = Files.newInputStream(path)) { + * // use input stream + * } + * + * + *
For the complete source code see + * + * CreateInputStream.java. * *
You can set various attributes using * {@link com.google.cloud.storage.contrib.nio.CloudStorageOptions CloudStorageOptions} static * helpers: *
- * Files.write(csvPath, csvLines, StandardCharsets.UTF_8, - * withMimeType(MediaType.CSV_UTF8), - * withoutCaching());+ * Path path = Paths.get(URI.create("gs://bucket/lolcat.csv")); + * Files.write(path, csvLines, StandardCharsets.UTF_8, + * withMimeType("text/csv; charset=UTF-8"), + * withoutCaching()); + * + * + *
For the complete source code see + * + * WriteFileWithAttributes.java. * *
NOTE: Cloud Storage uses a flat namespace and therefore doesn't support real * directories. So this library supports what's known as "pseudo-directories". Any path that @@ -67,32 +79,22 @@ * would with the normal UNIX file system implementation. You can disable this feature with * {@link com.google.cloud.storage.contrib.nio.CloudStorageConfiguration#usePseudoDirectories()}. * - *
Here's a simple unit test:
- * - * class MyTest { - * {@literal @}Rule - * public final AppEngineRule appEngine = AppEngineRule.builder().build(); - * - * {@literal @}Test - * public test_fileWrite() throws Exception { - * Path path = Paths.get(URI.create("gs://bucket/traditional")); - * Files.write(path, "eyebrow".getBytes(StandardCharsets.US_ASCII)); - * assertEquals("eyebrow", new String(Files.readBytes(path), StandardCharsets.US_ASCII)); - * } - * }- * *
If you don't want to rely on Java SPI, which requires a META-INF file in your jar generated by - * Google Auto, you can instantiate this file system directly as follows:
{@code + * Google Auto, you can instantiate this file system directly as follows: * - * CloudStorageFileSystem fs = CloudStorageFileSystemProvider.forBucket("bucket"); + *+ * + *+ * CloudStorageFileSystem fs = CloudStorageFileSystem.forBucket("bucket"); * byte[] data = "hello world".getBytes(StandardCharsets.UTF_8); * Path path = fs.getPath("/object"); * Files.write(path, data); - * data = Files.readBytes(path);}+ * data = Files.readAllBytes(path); + *
For the complete source code see
+ *
+ * CreateCloudStorageFileSystem.java.
*/
@javax.annotation.ParametersAreNonnullByDefault
package com.google.cloud.storage.contrib.nio;
diff --git a/gcloud-java-examples/src/main/java/com/google/cloud/examples/nio/snippets/CreateCloudStorageFileSystem.java b/gcloud-java-examples/src/main/java/com/google/cloud/examples/nio/snippets/CreateCloudStorageFileSystem.java
new file mode 100644
index 000000000000..b646fa4f1f90
--- /dev/null
+++ b/gcloud-java-examples/src/main/java/com/google/cloud/examples/nio/snippets/CreateCloudStorageFileSystem.java
@@ -0,0 +1,42 @@
+/*
+ * Copyright 2016 Google Inc. All Rights Reserved.
+ *
+ * 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.cloud.examples.nio.snippets;
+
+import com.google.cloud.storage.contrib.nio.CloudStorageFileSystem;
+
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+
+/**
+ * A snippet for Google Cloud Storage NIO that shows how to create a {@link CloudStorageFileSystem}
+ * for a bucket. The snippet also shows how to create a file, given the file system.
+ */
+public class CreateCloudStorageFileSystem {
+
+ public static void main(String... args) throws IOException {
+ // Create a file system for the bucket
+ CloudStorageFileSystem fs = CloudStorageFileSystem.forBucket("bucket");
+ byte[] data = "hello world".getBytes(StandardCharsets.UTF_8);
+ Path path = fs.getPath("/object");
+ // Write a file in the bucket
+ Files.write(path, data);
+ // Read a file from the bucket
+ data = Files.readAllBytes(path);
+ }
+}
diff --git a/gcloud-java-examples/src/main/java/com/google/cloud/examples/nio/snippets/CreateInputStream.java b/gcloud-java-examples/src/main/java/com/google/cloud/examples/nio/snippets/CreateInputStream.java
new file mode 100644
index 000000000000..243102d31e01
--- /dev/null
+++ b/gcloud-java-examples/src/main/java/com/google/cloud/examples/nio/snippets/CreateInputStream.java
@@ -0,0 +1,37 @@
+/*
+ * Copyright 2016 Google Inc. All Rights Reserved.
+ *
+ * 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.cloud.examples.nio.snippets;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URI;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+/**
+ * A snippet showing how to create an input stream for a Google Cloud Storage file using NIO.
+ */
+public class CreateInputStream {
+
+ public static void main(String... args) throws IOException {
+ Path path = Paths.get(URI.create("gs://bucket/lolcat.csv"));
+ try (InputStream input = Files.newInputStream(path)) {
+ // use input stream
+ }
+ }
+}
diff --git a/gcloud-java-examples/src/main/java/com/google/cloud/examples/nio/snippets/GetFileSystem.java b/gcloud-java-examples/src/main/java/com/google/cloud/examples/nio/snippets/GetFileSystem.java
new file mode 100644
index 000000000000..2819340fa14a
--- /dev/null
+++ b/gcloud-java-examples/src/main/java/com/google/cloud/examples/nio/snippets/GetFileSystem.java
@@ -0,0 +1,41 @@
+/*
+ * Copyright 2016 Google Inc. All Rights Reserved.
+ *
+ * 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.cloud.examples.nio.snippets;
+
+import java.io.IOException;
+import java.net.URI;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.FileSystem;
+import java.nio.file.FileSystems;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.List;
+
+/**
+ * A snippet showing how to get a {@link FileSystem} instance for a Google Cloud Storage bucket.
+ * This snippet also shows how to create a file and read its lines.
+ */
+public class GetFileSystem {
+
+ public static void main(String... args) throws IOException {
+ FileSystem fs = FileSystems.getFileSystem(URI.create("gs://bucket"));
+ byte[] data = "hello world".getBytes(StandardCharsets.UTF_8);
+ Path path = fs.getPath("/object");
+ Files.write(path, data);
+ List