javascalaopenstackopenstack4j

Get uploaded object URL using openstack4j


Currently I'm using openstack4j to put objects into a publicly readable container. The upload works fine (I can get the URL of the object by piecing it together), but I'd like to be able to get the public URL of the uploaded object straight from the library if possible.

Looking through the docs I found ObjectLocation, but that only gives me the path component of the object instead of the full URL. Is there any way for me to get the full URL of an object using openstack4j?

My code for uploading a file is:

os.objectStorage.objects.put(containerName, objectName, Payloads.create(someFile))

Note: I'm actually using openstack4j in scala, though that shouldn't really matter.


Solution

  • If you are using OpenStack4j 3.0.X and Openstack Identity API v3 you can use this code to get the object-storage (swift) endpoint url:

    for (Service s : os.getToken().getCatalog()) {
                if (s.getName().equals("swift")) {
                    for (Endpoint e : s.getEndpoints()) {
                        if (e.getRegion().equals("dallas") && e.getIface().equals(Facing.PUBLIC)) {
                            objectUrl = e.getUrl().toString();
                        }
                    }
                }
            }
    

    Where os is an instance of org.openstack4j.api.OSClient.OSClientV3. You should look for the region that is mentioned in your credentials (which in my case is "dallas").

    Once you have the url for this endpoint you can use it to create the URLs for your objects:

    objectUrl + "/" + containerName + "/" + objectName
    

    This will result in (just as an example, it doesn't link to an actual object):

    https://dal.objectstorage.open.softlayer.com/v1/AUTH_2440e4fa175452cb2e14506cb5d63ec/MyContainer/uploadedOpbject.jpg