Skip to content

Instantly share code, notes, and snippets.

@bijukunjummen
Created May 9, 2023 21:55
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 bijukunjummen/f541add19bac5af89b4d8454fba62dc0 to your computer and use it in GitHub Desktop.
Save bijukunjummen/f541add19bac5af89b4d8454fba62dc0 to your computer and use it in GitHub Desktop.
public void upload() {
byte[] buffer = new byte[4096];
BlobInfo build = BlobInfo.newBuilder("some-bucket", "samplefile1.txt").build();
Field uploadIdField = BaseWriteChannel.class.getDeclaredField("uploadId");
ReflectionUtils.makeAccessible(uploadIdField);
try (WriteChannel writer = storage.writer(build)) {
String uploadId = (String) ReflectionUtils.getField(uploadIdField, writer);
String changedHostUploadId = changeHost(uploadId, "myinternalhost.test.com");
ReflectionUtils.setField(uploadIdField, writer, changedHostUploadId);
try (InputStream input = this.getClass().getResourceAsStream("/samplefile.txt")) {
int limit;
while ((limit = input.read(buffer)) >= 0) {
writer.write(ByteBuffer.wrap(buffer, 0, limit));
}
}
}
}
private String changeHost(String uploadId, String newAuthority) {
URI uri = URI.create(uploadId);
try {
URI newUri = new URI(uri.getScheme(), newAuthority, uri.getPath(), uri.getQuery(), uri.getFragment());
return newUri.toString();
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment