-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Get token for PlanetaryComputer resources
!build-snapshot
- Loading branch information
1 parent
c5b69c9
commit 3965ead
Showing
2 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
46 changes: 46 additions & 0 deletions
46
gears/src/main/java/org/hortonmachine/gears/io/stac/PlanetaryComputerMicrosoft.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package org.hortonmachine.gears.io.stac; | ||
|
||
import org.json.JSONObject; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.URI; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
|
||
public class PlanetaryComputerMicrosoft { | ||
/** | ||
* Planetary Computer assets are usually stored as Azure blobs. | ||
* @param href | ||
* @return | ||
*/ | ||
static boolean isAzureBlob(String href) { | ||
return href.contains("blob.core.windows.net"); | ||
} | ||
|
||
/** | ||
* A token is needed to access Planetary Computer assets under Azure blob. | ||
* This method gets an Azure blob href and returns a curated href for the same resource. | ||
* https://planetarycomputer.microsoft.com/docs/concepts/sas/ | ||
* @param href | ||
* @return href with token | ||
* @throws IOException | ||
* @throws InterruptedException | ||
*/ | ||
static String getHrefWithToken(String href) throws IOException, InterruptedException { | ||
String getReadAccess = "https://planetarycomputer.microsoft.com/api/sas/v1/sign?href=" + href; | ||
HttpClient client = HttpClient.newBuilder().build(); | ||
HttpRequest.Builder uriBuilder = HttpRequest.newBuilder() | ||
.uri(URI.create(getReadAccess)); | ||
HttpRequest request = uriBuilder.GET().build(); | ||
// Send the request and retrieve the response | ||
HttpResponse<InputStream> response = client.send(request, HttpResponse.BodyHandlers.ofInputStream()); | ||
int status = response.statusCode(); | ||
if (status != 200) { | ||
throw new IOException(new String(response.body().readAllBytes())); | ||
} | ||
JSONObject body = new JSONObject(new String(response.body().readAllBytes())); | ||
return body.getString("href"); | ||
} | ||
} |