Last active
April 23, 2017 18:38
-
-
Save salrashid123/6d18338e55ff19f7faf1c8142e5a3581 to your computer and use it in GitHub Desktop.
GCS SignedURL in java with metadata headers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> | |
<packaging>jar</packaging> | |
<version>1.0-SNAPSHOT</version> | |
<groupId>com.test.TestApp</groupId> | |
<artifactId>TestApp</artifactId> | |
<properties> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>com.google.cloud</groupId> | |
<artifactId>google-cloud</artifactId> | |
<version>0.12.0-alpha</version> | |
</dependency> | |
<dependency><groupId>commons-codec</groupId><artifactId>commons-codec</artifactId><version>1.9</version></dependency> | |
</dependencies> | |
<build> | |
<plugins> | |
<plugin> | |
<artifactId>maven-compiler-plugin</artifactId> | |
<version>2.0.2</version> | |
<configuration> | |
<source>1.7</source> | |
<target>1.7</target> | |
</configuration> | |
</plugin> | |
<plugin> | |
<groupId>org.codehaus.mojo</groupId> | |
<artifactId>exec-maven-plugin</artifactId> | |
<version>1.2.1</version> | |
<executions> | |
<execution> | |
<goals> | |
<goal>java</goal> | |
</goals> | |
</execution> | |
</executions> | |
<configuration> | |
<mainClass>com.test.TestApp</mainClass> | |
</configuration> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.test; | |
import com.google.cloud.storage.Bucket; | |
import com.google.cloud.storage.Storage; | |
import com.google.cloud.storage.StorageOptions; | |
import java.util.Collection; | |
import java.util.Iterator; | |
import java.io.FileInputStream; | |
import com.google.auth.oauth2.GoogleCredentials; | |
import com.google.auth.oauth2.ServiceAccountCredentials; | |
import com.google.auth.oauth2.ComputeEngineCredentials; | |
//import com.google.auth.oauth2.AppEngineCredentials; | |
import com.google.cloud.storage.Blob; | |
import com.google.cloud.storage.Blob.BlobSourceOption; | |
import com.google.cloud.storage.BlobId; | |
import com.google.cloud.storage.BlobInfo; | |
import com.google.cloud.storage.Storage.SignUrlOption; | |
import java.util.concurrent.TimeUnit; | |
import java.net.URL; | |
import java.util.*; | |
import com.google.cloud.storage.*; | |
import java.net.URL; | |
import java.util.*; | |
import java.util.List; | |
import java.util.TimeZone; | |
import java.util.logging.Logger; | |
import java.net.URLEncoder; | |
import java.util.concurrent.TimeUnit; | |
import org.apache.commons.codec.binary.Base64; | |
public class TestApp { | |
public static void main(String[] args) { | |
TestApp tc = new TestApp(); | |
} | |
public TestApp() { | |
try | |
{ | |
ServiceAccountCredentials creds = ServiceAccountCredentials.fromStream(new FileInputStream("/home/srashid/gcp_misc/certs/GCPNETAppID-e65deccae47b.json")); | |
Storage storage_service = StorageOptions.newBuilder() | |
.setCredentials(creds) | |
.build() | |
.getService(); | |
URL signedUrl = storage_service.signUrl( | |
BlobInfo.newBuilder("mineral-minutia-820", "a.txt") | |
.build(), | |
60, | |
TimeUnit.SECONDS, | |
Storage.SignUrlOption.httpMethod(HttpMethod.PUT)); | |
System.out.println(signedUrl); | |
// ------------------------------------------------------------------------------ | |
System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); | |
String BUCKET_NAME = "mineral-minutia-820"; | |
String OBJECT_NAME = "a.txt"; | |
String SERVICE_ACCOUNT_EMAIL = "[email protected]"; | |
String verb = "PUT"; | |
long expiration = System.currentTimeMillis()/1000 + 60; | |
String Canonicalized_Extension_Headers = "x-goog-meta-icecreamflavor:vanilla"; | |
byte[] sr = creds.sign( (verb + "\n\n\n" + expiration + "\n" + Canonicalized_Extension_Headers + | |
"\n" + "/" + BUCKET_NAME + "/" + OBJECT_NAME ).getBytes() ); | |
String url_signature = new String(Base64.encodeBase64( sr )); | |
String signed_url = "https://storage.googleapis.com/" + BUCKET_NAME + "/" + OBJECT_NAME + | |
"?GoogleAccessId=" + SERVICE_ACCOUNT_EMAIL + | |
"&Expires=" + expiration + | |
"&Signature=" + URLEncoder.encode(url_signature, "UTF-8"); | |
System.out.println(signed_url); | |
} | |
catch (Exception ex) { | |
System.out.println("Error: " + ex); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment