Skip to content

Instantly share code, notes, and snippets.

@Ikhiloya
Last active July 22, 2020 09:37
Show Gist options
  • Select an option

  • Save Ikhiloya/f817dda14d39924569083bc419c743d7 to your computer and use it in GitHub Desktop.

Select an option

Save Ikhiloya/f817dda14d39924569083bc419c743d7 to your computer and use it in GitHub Desktop.
public String[] uploadFile(MultipartFile multipartFile) throws IOException {
File file = convertMultiPartToFile(multipartFile);
Path filePath = file.toPath();
String objectName = generateFileName(multipartFile);
Storage storage = storageOptions.getService();
BlobId blobId = BlobId.of(bucketName, objectName);
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();
Blob blob = storage.create(blobInfo, Files.readAllBytes(filePath));
log.info("File " + filePath + " uploaded to bucket " + bucketName + " as " + objectName);
return new String[]{"fileUrl", objectName};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment