Created
May 9, 2023 21:55
-
-
Save bijukunjummen/f541add19bac5af89b4d8454fba62dc0 to your computer and use it in GitHub Desktop.
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
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