Skip to content

Instantly share code, notes, and snippets.

@mloza
Created April 21, 2020 19:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mloza/4f7953a73ec449f272613fe1f1d3b190 to your computer and use it in GitHub Desktop.
Save mloza/4f7953a73ec449f272613fe1f1d3b190 to your computer and use it in GitHub Desktop.
Kod do postu o Google Cloud Storage znajdujący się pod adresem https://blog.mloza.pl/google-cloud-storage/
public class CredentialsProvider {
public static Credential authorize() {
try {
return GoogleCredential.fromStream(CredentialsProvider.class.getClassLoader()
.getResourceAsStream("client-secrets.json"))
.createScoped(Collections.singleton(StorageScopes.CLOUD_PLATFORM));
} catch (IOException e) {
Throwables.propagate(e);
}
return null;
}
}
@Test
public void deleteObjectFromBucket() throws Exception {
storage.objects()
.delete("blog-test", "kot-z-serem-x2.jpg")
.execute();
listBucket();
}
@Test
public void getObjectFromBucket() throws Exception {
storage.objects()
.get("blog-test", "kot-z-serem.jpg") //1
.executeMediaAndDownloadTo(new FileOutputStream("kot-z-serem.jpg")); //2
}
public class GoogleCloudStorageTest {
private static final String APPLICATION_NAME = "Test Application";
private HttpTransport httpTransport;
private Credential credential;
private JsonFactory jsonFactory;
private Storage storage;
@BeforeClass
public void setUp() throws Exception {
try {
httpTransport = GoogleNetHttpTransport.newTrustedTransport();
credential = CredentialsProvider.authorize();
jsonFactory = new JacksonFactory();
storage = new Storage.Builder(httpTransport, jsonFactory, credential).setApplicationName(APPLICATION_NAME).build();
} catch (GeneralSecurityException | IOException e) {
e.printStackTrace();
}
}
@Test
public void listBuckets() throws Exception {
storage.buckets()
.list("test-ocr-on-googl")
.execute()
.getItems()
.forEach(i -> System.out.println(i.getName()));
}
}
@Test
public void listBucket() throws Exception {
storage.objects()
.list("blog-test") // 1
.execute()
.getItems()
.forEach(i -> System.out.println(i.getName()));
}
@Test
public void listBucketWithPrefix() throws Exception {
storage.objects()
.list("blog-test")
.setPrefix("fol") // 1
.execute()
.getItems()
.forEach(i -> System.out.println(i.getName()));
}
public class Main {
public static void main(String[] args) {
try {
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory jsonFactory = new JacksonFactory();
Credential credential = GoogleCredential
.fromStream(
Main.class.getClassLoader()
.getResourceAsStream("client-secrets.json")) // 1
.createScoped(Collections.singleton(StorageScopes.CLOUD_PLATFORM)); // 2
Storage storage = new Storage.Builder(httpTransport, jsonFactory, credential)
.setApplicationName("Test project") // 3
.build();
storage.buckets()
.list("blog-test")
.execute()
.getItems()
.forEach(i -> System.out.println(i.getName())); // 4
} catch (GeneralSecurityException | IOException e) {
e.printStackTrace();
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>pl.mloza</groupId>
<artifactId>google-cloud-storage</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-storage</artifactId>
<version>v1-rev72-1.22.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
@Test
public void uploadObjectToBucket() throws Exception {
InputStreamContent mediaContent = new InputStreamContent("image/jpeg", new FileInputStream("kot-z-serem-x.jpg")); // 1
storage.objects()
.insert("blog-test", null, mediaContent) // 2
.setName("kot-z-serem-x.jpg") // 3
.execute();
listBucket(); //4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment