openstack4j icon indicating copy to clipboard operation
openstack4j copied to clipboard

Object Storage: Download object and write to file

Open lsarni opened this issue 8 years ago • 9 comments

I'm using this to download an object and write it to a file: client.objectStorage().objects().download(publicBucket, externalFileName).writeToFile(new File(localFile));

I was expecting it to fail with a StorageException if the object didn't exist but this is not the case, it creates a file of 70 bytes. Is this the expected outcome? I was expecting the http response to be not found and that to be in the storage exception.

lsarni avatar Jan 11 '17 17:01 lsarni

Mh. At first glance no. It's unexpected. Could you check the response from Swift, @lsarni ?

auhlig avatar Jan 11 '17 21:01 auhlig

Using this I get:

HTTP/1.1 404 Not Found
Content-Length: 70
Content-Type: text/html; charset=UTF-8
X-Trans-Id: tx0b057418fd4d4de2aac6c-0058777297
Date: Thu, 12 Jan 2017 12:12:07 GMT

<html><h1>Not Found</h1><p>The resource could not be found.</p></html>

I also tried it on a console app, outside my main project, and it still downloads a file.

lsarni avatar Jan 12 '17 12:01 lsarni

Looking at the content length, I guess the response is written to the file?

auhlig avatar Jan 12 '17 14:01 auhlig

Yes, I was trying to download an image but if I download a txt the content is:

<html><h1>Not Found</h1><p>The resource could not be found.</p></html>

lsarni avatar Jan 12 '17 14:01 lsarni

Mh. It should return null, as the download() method leads to get(), looking at https://github.com/ContainX/openstack4j/blob/master/core/src/main/java/org/openstack4j/openstack/storage/object/internal/ObjectStorageObjectServiceImpl.java#L82 . What happens if you just try to download? Whiteout writing to file) I guess you found a bug if that's not working.

auhlig avatar Jan 12 '17 19:01 auhlig

If I download without writing to the file it doesn't fail either, it returns a DLPayload that has an HttpResponse saying 404 Not Found.

lsarni avatar Jan 13 '17 12:01 lsarni

Ok. I'll mark it as a bug. Since you're already familiar with the issue, would you like to contribute and fix it @lsarni? Would be much appreciated.

auhlig avatar Jan 13 '17 17:01 auhlig

I think when the request does not read the entity from the response, openstack4j does not handle exceptions at all. I think if any error response received from the server, all connectors should call HttpExceptionHandler.mapException for example at HttpExecutorServiceImpl.invokeRequest like what has written in HttpEntityHandler. this will throw an exception on any failure. If it is the preferred outcome, I can fix it. Otherwise please let me know what was the original idea and how should such an error be handled? @auhlig

hossein-nasr avatar May 11 '19 10:05 hossein-nasr

Meanwhile, I did what I said and found out that this is not how openstack4j works because multiple tests had failed because of my change. So error response should not throw an exception which is still weird for me because it questions the existence of ResponseException. So, I think the ObjectStorageService interface should be changed to let the caller access to the error and response headers. I think we should add some methods which return the HttpResponse. I appreciate if anyone could help me we openstack4j general design about this issue.

hossein-nasr avatar May 11 '19 11:05 hossein-nasr