upload a file to Object Storage using Spring RestTemplate

I have tried with this piece of code, based on https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutObject.html

    Resource resource = new ClassPathResource("logback.xml");
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.TEXT_PLAIN);

    HttpEntity<byte[]> requestEntity
            = new HttpEntity<>(StreamUtils.copyToByteArray(resource.getInputStream()), headers);

    Map<String, Object> parameters = new HashMap<>(4);
    parameters.put("cors_enabled", true);
    parameters.put("acl", "private");
    parameters.put("key", "my-key");
    parameters.put("Bucket", "https://cloud.linode.com/object-storage/buckets/eu-central-1/my-bucket-2020");

    restTemplate.put("https://api.linode.com/v4/object-storage/buckets", requestEntity, parameters);

but I have this response:

 org.springframework.web.client.HttpClientErrorException$MethodNotAllowed: 405 METHOD NOT ALLOWED: [{"errors": [{"reason": "Method Not Allowed"}]}]

I have also problems when getting an object:

       MultiValueMap<String, Object> body
            = new LinkedMultiValueMap<>();

    UriComponentsBuilder builder =
            UriComponentsBuilder.fromHttpUrl("https://api.linode.com/v4/object-storage/buckets/eu-central-1/my-bucket-2020/object-url");

    body.add("method", "GET");
    body.add("name", "43f959d930d-a11a-4f2cec88fd7e.JPG");

    HttpHeaders headers = new HttpHeaders();

    HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);

    restTemplate.postForEntity(builder.build().encode().toUri(),
            requestEntity, LinodeResponse.class);

with this responde:

          org.springframework.web.client.HttpClientErrorException$BadRequest: 400 BAD REQUEST: [{"errors": [{"reason": "name is required", "field": "name"}, {"reason": "method is required", "field": "method"}]}]

1 Reply

Hello!

I've not used this template before, but I wanted to try to help you out so I did some digging around online and found some resources that might give some insight into this for you.

I found this page which discusses the 405 error that you're getting, and it gives an answer that might be helpful:

RestTemplate- 405 Method Not Allowed

The second I found looks like it's showing the same error you're getting and there's a suggested fix. The solution hasn't received any upvotes as of my writing of this response, but it's worth taking a look at:

S3 Upload Using RestTemplate

I see you've added some good tags to this post, too, so this should get some visibility with the rest of the Linode Community.

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct