1 /*
2  * Copyright 2016 Google LLC
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *       http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.cloud.examples.nio.snippets;
18 
19 import static com.google.cloud.storage.contrib.nio.CloudStorageOptions.withMimeType;
20 import static com.google.cloud.storage.contrib.nio.CloudStorageOptions.withoutCaching;
21 
22 import com.google.cloud.storage.contrib.nio.CloudStorageOptions;
23 import java.io.IOException;
24 import java.net.URI;
25 import java.nio.charset.StandardCharsets;
26 import java.nio.file.Files;
27 import java.nio.file.Path;
28 import java.nio.file.Paths;
29 import java.util.Arrays;
30 import java.util.List;
31 
32 /**
33  * A snippet showing how to write a file to Google Cloud Storage using NIO. This example also shows
34  * how to set file attributes, using {@link CloudStorageOptions} static helpers.
35  */
36 public class WriteFileWithAttributes {
37 
38   private static final String[] LINES = {"value1,", "value"};
39 
main(String... args)40   public static void main(String... args) throws IOException {
41     List<String> csvLines = Arrays.asList(LINES);
42     Path path = Paths.get(URI.create("gs://bucket/lolcat.csv"));
43     Files.write(
44         path,
45         csvLines,
46         StandardCharsets.UTF_8,
47         withMimeType("text/csv; charset=UTF-8"),
48         withoutCaching());
49   }
50 }
51