2

I'm converting a small Spring method to Micronaut. The purpose of this method is to write a byte array to a destination in the cloud. To identify the destination, a URI is passed as argument to the Spring method. This is the implementation using Spring:

    public class MyWriter{

        public MyWriter(ResourceLoader resourceLoader) {
            this.resourceLoader = resourceLoader;
        }
    
        public boolean writeToDestination(String uri, byte[] bytes) {
        
            WritableResource resource = (WritableResource) resourceLoader.getResource(uri);
    
            try (OutputStream out = resource.getOutputStream()) {
                out.write(bytes);
            } catch (FileNotFoundException e) {
                return false;
            } catch (IOException e) {
                throw new WriteFileException(e);
            }
    
            return true;
         }

    }

Based on this question, I know that Micronaut doesn't have the org.springframework.core.io.Resource class, but it has io.micronaut.core.io.ResourceLoader and other variants. I have not found any Micronaut alternative to org.springframework.core.io.WritableResource.

I have come across io.micronaut:micronaut-cli:2.0.0.M2, which apparently includes a similar implementation to the Spring classes Resource and WritableResource, but every time I use that I get this error:

Failed to inject value for parameter [resourceLoader] of class: ResourceManager    
Message: No bean of type [io.micronaut.cli.io.support.ResourceLoader] exists.

I am new to Micronaut, so I am not familiar with most IO features it offers.

That being said, how do I convert the method above to Micronaut?

m-oliv
  • 419
  • 11
  • 27
  • "The purpose of this method is to write a byte array to a destination, which could be a local file or something else in the cloud." - Would you like to see an example of writing bytes to a local file, or to something else in the cloud? – Jeff Scott Brown Nov 01 '22 at 18:49
  • @JeffScottBrown Ideally, I'd have a look at both because the method should write to the cloud but also should be testable locally (and in that case I'd use the local filesystem). – m-oliv Nov 02 '22 at 12:33
  • "I'd have a look at both" - The best practice for each will be different, and some might accept a proposed approach for one and not for the other. Your question would be better served as 2 separate questions, IMO. – Jeff Scott Brown Nov 02 '22 at 13:08
  • @JeffScottBrown I gave this some more thought and edited the question to clarify that I am interested in writing to the cloud. – m-oliv Nov 08 '22 at 15:48
  • Do you want to write the bytes to a relational database in the cloud, some cloud storage like an S3 bucket, or something else? – Jeff Scott Brown Nov 08 '22 at 20:03
  • @JeffScottBrown I want to write the bytes to a Google Cloud Bucket. – m-oliv Nov 09 '22 at 08:29
  • 1
    "I want to write the bytes to a Google Cloud Bucket. " - FYI, we have published a guide at https://guides.micronaut.io/latest/micronaut-object-storage-gcp-gradle-java.html which describes how to do that. – Jeff Scott Brown Nov 09 '22 at 14:31
  • I believe I have answered your question. Can you check, upvote and accept it if it meets your requirements, thanks – dangarfield Nov 16 '22 at 12:49

1 Answers1

1

If it's cloud storage specifically, you should try using Micronaut Object Storage.

Eg: Install dependencies for your cloud provider

<dependency>
    <groupId>io.micronaut.objectstorage</groupId>
    <artifactId>micronaut-object-storage-aws</artifactId>
    <version>1.1.0</version>
</dependency>

Set configuration options - src/main/resources/application-ec2.yml

micronaut:
  object-storage:
    aws:
      default:
        bucket: profile-pictures-bucket

Inject in your controllers/services/etc. a bean of type ObjectStorageOperations, the parent interface that allows you to use the API in a generic way for all cloud providers.

And use:

public String saveProfilePicture(String userId, Path path) {
    UploadRequest request = UploadRequest.fromPath(path, userId);
    UploadResponse<?> response = objectStorage.upload(request);
    return response.getKey();
}

All info above is from: Micronaut Object Storage.

dangarfield
  • 2,210
  • 1
  • 13
  • 18