none
[Java][Azure Storage] Data truncated while writing to azure blob RRS feed

  • Question

  • We are using the following java code to create files on blob storage. The input data (dataString) being passed is a JSON string. The length of the string is 37270 characters. The string gets truncated while being written to the blob.

    Is there any limitation to the number of characters supported by the upload API.

    Has anyone face a similar situation?

    Thank you.

    public  URI  uploadBlobAsString(String dataString,
       CloudStorageAccount storageAccount, String containerName,
       String referenceName) throws Exception {
    
      CloudBlockBlob blob = null;
      CloudBlobClient blobClient = null;
      CloudBlobContainer container = null;
      blobClient = storageAccount.createCloudBlobClient();
      container = blobClient.getContainerReference(containerName);
    
      //container.createIfNotExist();
    
      blob = container.getBlockBlobReference(referenceName);
    
      InputStream inputStream = null;
      try {
       inputStream = new ByteArrayInputStream(dataString.getBytes("UTF-8"));
       blob.upload(inputStream,dataString.length());
       
      } catch (Exception exception) {
       throw exception;
      }finally {
       try {
        if(inputStream!=null) {
         inputStream.close();
         inputStream=null;
        }
       }catch(Exception exception) {}
      }
      return blob.getUri();
     }



    Monday, January 11, 2016 12:37 PM

Answers

  • Per my experience, the issue was caused by the code  `dataString.length()`, because using UTF-8 to encode string for different languages will return 1-byte, 2-bytes, 3-bytes per character. So there shoud be the code `dataString.getBytes("UTF-8").length` instead of the code `dataString.length()`.

    Otherwise, I think you can try to use `StringBufferInputStream` as `InputStream` for the function `upload`, please see below.
    inputStream = new StringBufferInputStream(dataString);
    blob.upload(inputStream, dataString.length());
    Tuesday, January 12, 2016 3:50 AM
    Moderator