use of com.linecorp.armeria.client.InvalidResponseException in project curiostack by curioswitch.
the class StorageClient method readFile.
/**
* Reads the contents of a file from cloud storage. Ownership of the returned {@link ByteBuf} is
* transferred to the caller, which must release it. The future will complete with {@code null} if
* the file is not found.
*/
public CompletableFuture<ByteBuf> readFile(String filename, EventLoop eventLoop, ByteBufAllocator alloc) {
String url = objectUrlPrefix + urlPathSegmentEscaper().escape(filename) + "?alt=media";
return httpClient.get(url).aggregateWithPooledObjects(eventLoop, alloc).thenApply(msg -> {
if (msg.status().equals(HttpStatus.NOT_FOUND)) {
ReferenceCountUtil.safeRelease(msg.content());
return null;
}
if (!msg.status().equals(HttpStatus.OK)) {
String response = msg.contentUtf8();
ReferenceCountUtil.safeRelease(msg.content());
throw new InvalidResponseException("Could not fetch file at " + filename + ": " + response);
}
HttpData data = msg.content();
if (data instanceof ByteBufHolder) {
return ((ByteBufHolder) msg.content()).content();
} else {
ByteBuf buf = alloc.buffer(data.length());
buf.writeBytes(data.array());
return buf;
}
});
}
Aggregations