Search code examples
javagoogle-cloud-platformgoogle-cloud-storagegcloudpre-signed-url

Metadata, content-length for GCS objects


Two things:

  1. I am trying to set custom metadata on a GCS object signed URL.
  2. I am trying to set a maximum file size on a GCS object signed URL.

Using the following code:

    Map<String, String> headers = new HashMap<>();
    headers.put("x-goog-meta-" + usernameKey, username);

    if (StringUtils.hasText(purpose)) {
      headers.put("x-goog-meta-" + purposeKey, purpose);
    }

    if (maxFileSizeMb != null) {
      headers.put("x-goog-content-length-range", String.format("0,%d", maxFileSizeMb * 1048576));
    }

    List<Storage.SignUrlOption> options = new ArrayList<>();
    options.add(Storage.SignUrlOption.httpMethod(HttpMethod.POST));
    options.add(Storage.SignUrlOption.withExtHeaders(headers));

    String documentId = documentIdGenerator.generateDocumentId().getFormatted();
    StorageDocument storageDocument =
        StorageDocument.builder().id(documentId).path(getPathByDocumentId(documentId)).build();
    storageDocument.setFormattedName(documentId);

    SignedUrlData.SignedUrlDataBuilder builder =
        SignedUrlData.builder()
            .signedUrl(storageInterface.signUrl(gcpStorageBucket, storageDocument, options))
            .documentId(documentId)
            .additionalHeaders(headers);

First of all the generated signed URL works and I can upload a document.

Now I am expecting to see the object metadata through the console view. There is no metadata set though. Also the content-length-range is not respected. I can upload a 1.3 MB file when the content-length-range is set to 0,1.

Something happens when I upload a bigger file (~ 5 MB), but within the content-length-range. I receive an error message: Metadata part is too large..


Solution

  • As you can see here content-length-range requires both a minimum and maximum size. The unit used for the range is bytes, as you can see in this example.

    I also noticed that you used x-goog-content-length-range, I found this documentation for it, when using this header take into account:

    1. Use a PUT request, otherwise it will be silently ignored.
    2. If the size of the request's content is outside the specified range, the request fails and a 400 Bad Request code is returned in the response.
    3. You have to set the minimum and maximum size in bytes.