Skip to content

Commit

Permalink
Get token for PlanetaryComputer resources
Browse files Browse the repository at this point in the history
!build-snapshot
  • Loading branch information
inigo-cobian committed Jan 23, 2025
1 parent c5b69c9 commit 3965ead
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ public GridCoverage2D readRaster( RegionMap region, String user, String password
InputStream inputProvider = readS3Raster(cogUri, s3Client);
reader = new GeoTiffReader(inputProvider);
} else {
if (PlanetaryComputerMicrosoft.isAzureBlob(assetUrl)) {
String accessibleHref = PlanetaryComputerMicrosoft.getHrefWithToken(assetUrl);
cogUri = new BasicAuthURI(accessibleHref, false);
}
RangeReader rangeReader = new HttpRangeReader(cogUri.getUri(), CogImageReadParam.DEFAULT_HEADER_LENGTH);
CogSourceSPIProvider inputProvider = new CogSourceSPIProvider(cogUri, new CogImageReaderSpi(),
new CogImageInputStreamSpi(), rangeReader.getClass().getName());
Expand Down
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");
}
}

0 comments on commit 3965ead

Please sign in to comment.