javaopenstack-swiftjclouds

Uploading to Swift Container with Metadata


im using Java and the jclouds SDK to upload files to a Swift container as multipart, the uploading is going fine yet i need to add metadata to the file, i noticed that there is a function called getContentMetadata() which can get a metadata such as content length and type, yet i was unable to add custom metadata, tried to cast put options to metadata, didn't generate error but that generated an exception when i ran the code, the code is here

try {
        PutOptions putOptions = PutOptions.Builder.metadata(ImmutableMap.of("test", String.valueOf(strValue)));
        ByteSource fileBytes = Files.asByteSource(file);
        Payload payload = Payloads.newByteSourcePayload(fileBytes);
        ///setting the header for the request
        payload.getContentMetadata().setContentLength(file.length());
        payload.getContentMetadata().setContentType(contentType);
        payload.setContentMetadata((MutableContentMetadata) putOptions);

        Blob blob = blobStore.blobBuilder(file.getName()).payload(payload).build();
        ///sednig the request

        blobStore.putBlob("testContainer", blob, multipart());
        return contentLength;
    }

the line payload.setContentMetadata((MutableContentMetadata) putOptions); generated an exception

any idea how to solve this? Thanks


Solution

  • You should set metadata via BlobBuilder, e.g.,

    Blob blob = blobStore.blobBuilder(file.getName())
        .payload(fileBytes)
        .contentLength(file.length()
        .contentType(contentType)
        .userMetadata(ImmutableMap.of("test", String.valueOf(strValue)))
        .build();