openstack4j
openstack4j copied to clipboard
Object Storage: Download object and write to file
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.
Mh. At first glance no. It's unexpected. Could you check the response from Swift, @lsarni ?
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.
Looking at the content length, I guess the response is written to the file?
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>
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.
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.
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.
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
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.