Java Code Examples for com.microsoft.azure.storage.blob.CloudBlockBlob#uploadText()

The following examples show how to use com.microsoft.azure.storage.blob.CloudBlockBlob#uploadText() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CloudFileTests.java    From azure-storage-android with Apache License 2.0 7 votes vote down vote up
@Test
@Category(SlowTests.class)
public void testCopyBlockBlobSas() throws Exception {
    // Create source on server.
    final CloudBlobContainer container = BlobTestHelper.getRandomContainerReference();
    try {
        container.create();
        final CloudBlockBlob source = container.getBlockBlobReference("source");

        source.getMetadata().put("Test", "value");
        final String data = "String data";
        source.uploadText(data, Constants.UTF8_CHARSET, null, null, null);

        final CloudFile destination = doCloudBlobCopy(source, data.length());

        final String copyData = destination.downloadText(Constants.UTF8_CHARSET, null, null, null);
        assertEquals(data, copyData);
    }
    finally {
        container.deleteIfExists();
    }
}
 
Example 2
Source File: CloudAnalyticsClientTests.java    From azure-storage-android with Apache License 2.0 4 votes vote down vote up
/**
 * Validate Log Parser
 *
 * @throws ParseException
 * @throws URISyntaxException
 * @throws StorageException
 * @throws IOException
 * @throws InterruptedException
 */
@Test
public void testCloudAnalyticsClientParseExLogs() throws ParseException, URISyntaxException, StorageException,
        IOException {
    String logText = "1.0;2011-08-09T18:52:40.9241789Z;GetBlob;AnonymousSuccess;200;18;10;anonymous;;myaccount;blob;\"https://myaccount.blob.core.windows.net/thumb&nails/lake.jpg?timeout=30000\";\"/myaccount/thumbnails/lake.jpg\";a84aa705-8a85-48c5-b064-b43bd22979c3;0;123.100.2.10;2009-09-19;252;0;265;100;0;;;\"0x8CE1B6EA95033D5\";Tuesday, 09-Aug-11 18:52:40 GMT;;;;\"8/9/2011 6:52:40 PM ba98eb12-700b-4d53-9230-33a3330571fc\""
            + '\n'
            + "1.0;2011-08-09T18:02:40.6271789Z;PutBlob;Success;201;28;21;authenticated;myaccount;myaccount;blob;\"https://myaccount.blob.core.windows.net/thumbnails/lake.jpg?timeout=30000\";\"/myaccount/thumbnails/lake.jpg\";fb658ee6-6123-41f5-81e2-4bfdc178fea3;0;201.9.10.20;2009-09-19;438;100;223;0;100;;\"66CbMXKirxDeTr82SXBKbg==\";\"0x8CE1B67AD25AA05\";Tuesday, 09-Aug-11 18:02:40 GMT;;;;\"8/9/2011 6:02:40 PM ab970a57-4a49-45c4-baa9-20b687941e32\""
            + '\n';
    this.container.createIfNotExists();
    CloudBlockBlob blob = this.container.getBlockBlobReference("blob1");
    blob.uploadText(logText);
    Iterator<LogRecord> iterator = CloudAnalyticsClient.parseLogBlob(blob).iterator();

    assertTrue(iterator.hasNext());
    LogRecord actualItemOne = iterator.next();
    assertTrue(iterator.hasNext());
    LogRecord actualItemTwo = iterator.next();

    LogRecord expectedItemOne = new LogRecord();
    expectedItemOne.setVersionNumber("1.0");
    expectedItemOne.setRequestStartTime(LogRecord.REQUEST_START_TIME_FORMAT.parse("2011-08-09T18:52:40.9241789Z"));
    expectedItemOne.setOperationType("GetBlob");
    expectedItemOne.setRequestStatus("AnonymousSuccess");
    expectedItemOne.setHttpStatusCode("200");
    expectedItemOne.setEndToEndLatencyInMS(18);
    expectedItemOne.setServerLatencyInMS(10);
    expectedItemOne.setAuthenticationType("anonymous");
    expectedItemOne.setRequesterAccountName(null);
    expectedItemOne.setOwnerAccountName("myaccount");
    expectedItemOne.setServiceType("blob");
    expectedItemOne.setRequestUrl(new URI(
            "https://myaccount.blob.core.windows.net/thumb&nails/lake.jpg?timeout=30000"));
    expectedItemOne.setRequestedObjectKey("/myaccount/thumbnails/lake.jpg");
    expectedItemOne.setRequestIdHeader(UUID.fromString("a84aa705-8a85-48c5-b064-b43bd22979c3"));
    expectedItemOne.setOperationCount(0);
    expectedItemOne.setRequesterIPAddress("123.100.2.10");
    expectedItemOne.setRequestVersionHeader("2009-09-19");
    expectedItemOne.setRequestHeaderSize(252L);
    expectedItemOne.setRequestPacketSize(0L);
    expectedItemOne.setResponseHeaderSize(265L);
    expectedItemOne.setResponsePacketSize(100L);
    expectedItemOne.setRequestContentLength(0L);
    expectedItemOne.setRequestMD5(null);
    expectedItemOne.setServerMD5(null);
    expectedItemOne.setETagIdentifier("0x8CE1B6EA95033D5");
    expectedItemOne.setLastModifiedTime(LogRecord.LAST_MODIFIED_TIME_FORMAT
            .parse("Tuesday, 09-Aug-11 18:52:40 GMT"));
    expectedItemOne.setConditionsUsed(null);
    expectedItemOne.setUserAgentHeader(null);
    expectedItemOne.setReferrerHeader(null);
    expectedItemOne.setClientRequestId("8/9/2011 6:52:40 PM ba98eb12-700b-4d53-9230-33a3330571fc");

    LogRecord expectedItemTwo = new LogRecord();
    expectedItemTwo.setVersionNumber("1.0");
    expectedItemTwo.setRequestStartTime(LogRecord.REQUEST_START_TIME_FORMAT.parse("2011-08-09T18:02:40.6271789Z"));
    expectedItemTwo.setOperationType("PutBlob");
    expectedItemTwo.setRequestStatus("Success");
    expectedItemTwo.setHttpStatusCode("201");
    expectedItemTwo.setEndToEndLatencyInMS(28);
    expectedItemTwo.setServerLatencyInMS(21);
    expectedItemTwo.setAuthenticationType("authenticated");
    expectedItemTwo.setRequesterAccountName("myaccount");
    expectedItemTwo.setOwnerAccountName("myaccount");
    expectedItemTwo.setServiceType("blob");
    expectedItemTwo.setRequestUrl(new URI(
            "https://myaccount.blob.core.windows.net/thumbnails/lake.jpg?timeout=30000"));
    expectedItemTwo.setRequestedObjectKey("/myaccount/thumbnails/lake.jpg");
    expectedItemTwo.setRequestIdHeader(UUID.fromString("fb658ee6-6123-41f5-81e2-4bfdc178fea3"));
    expectedItemTwo.setOperationCount(0);
    expectedItemTwo.setRequesterIPAddress("201.9.10.20");
    expectedItemTwo.setRequestVersionHeader("2009-09-19");
    expectedItemTwo.setRequestHeaderSize(438L);
    expectedItemTwo.setRequestPacketSize(100L);
    expectedItemTwo.setResponseHeaderSize(223L);
    expectedItemTwo.setResponsePacketSize(0L);
    expectedItemTwo.setRequestContentLength(100L);
    expectedItemTwo.setRequestMD5(null);
    expectedItemTwo.setServerMD5("66CbMXKirxDeTr82SXBKbg==");
    expectedItemTwo.setETagIdentifier("0x8CE1B67AD25AA05");
    expectedItemTwo.setLastModifiedTime(LogRecord.LAST_MODIFIED_TIME_FORMAT
            .parse("Tuesday, 09-Aug-11 18:02:40 GMT"));
    expectedItemTwo.setConditionsUsed(null);
    expectedItemTwo.setUserAgentHeader(null);
    expectedItemTwo.setReferrerHeader(null);
    expectedItemTwo.setClientRequestId("8/9/2011 6:02:40 PM ab970a57-4a49-45c4-baa9-20b687941e32");

    CloudAnalyticsClientTests.assertLogItemsEqual(expectedItemOne, actualItemOne);
    CloudAnalyticsClientTests.assertLogItemsEqual(expectedItemTwo, actualItemTwo);
}
 
Example 3
Source File: FileSasTests.java    From azure-storage-android with Apache License 2.0 4 votes vote down vote up
@Test
public void testFileCopyFromBlobWithSasAndSnapshot()
        throws URISyntaxException, StorageException, InterruptedException, IOException, InvalidKeyException {
    String blobName = BlobTestHelper.generateRandomBlobNameWithPrefix("testblob");
    CloudBlobContainer container = TestHelper.createCloudBlobClient().getContainerReference(BlobTestHelper.generateRandomContainerName());
    container.createIfNotExists();
    CloudBlockBlob source = container.getBlockBlobReference(blobName);
    String data = "String data";
    source.uploadText(data, Constants.UTF8_CHARSET, null, null, null);

    byte[] buffer = BlobTestHelper.getRandomBuffer(512);
    ByteArrayInputStream stream = new ByteArrayInputStream(buffer);
    source.upload(stream, buffer.length);
    source.getMetadata().put("Test", "value");
    source.uploadMetadata();

    SharedAccessFilePolicy policy = createSharedAccessPolicy(
            EnumSet.of(SharedAccessFilePermissions.READ, SharedAccessFilePermissions.WRITE,
                  SharedAccessFilePermissions.LIST, SharedAccessFilePermissions.DELETE), 5000);

    CloudFile copy = this.share.getRootDirectoryReference().getFileReference("copy");
    String sasToken = copy.generateSharedAccessSignature(policy, null);
    CloudFile copySas = new CloudFile(new URI(copy.getUri().toString() + "?" + sasToken));
    
    // Generate account SAS for the source
    // Cannot generate a SAS directly on a snapshot and the SAS for the destination is only for the destination
    SharedAccessAccountPolicy accountPolicy = new SharedAccessAccountPolicy();
    accountPolicy.setPermissions(EnumSet.of(SharedAccessAccountPermissions.READ, SharedAccessAccountPermissions.WRITE));
    accountPolicy.setServices(EnumSet.of(SharedAccessAccountService.BLOB));
    accountPolicy.setResourceTypes(EnumSet.of(SharedAccessAccountResourceType.OBJECT, SharedAccessAccountResourceType.CONTAINER));
    accountPolicy.setSharedAccessExpiryTime(policy.getSharedAccessExpiryTime());
    final CloudBlobClient sasClient = TestHelper.createCloudBlobClient(accountPolicy, false);

    CloudBlockBlob snapshot = (CloudBlockBlob) source.createSnapshot();
    CloudBlockBlob sasBlob = (CloudBlockBlob) sasClient.getContainerReference(container.getName())
            .getBlobReferenceFromServer(snapshot.getName(), snapshot.getSnapshotID(), null, null, null);
    sasBlob.exists();

    String copyId = copySas.startCopy(BlobTestHelper.defiddler(sasBlob));
    FileTestHelper.waitForCopy(copySas);
    
    copySas.downloadAttributes();
    FileProperties prop1 = copySas.getProperties();
    BlobProperties prop2 = sasBlob.getProperties();

    assertEquals(prop1.getCacheControl(), prop2.getCacheControl());
    assertEquals(prop1.getContentEncoding(), prop2.getContentEncoding());
    assertEquals(prop1.getContentDisposition(),
            prop2.getContentDisposition());
    assertEquals(prop1.getContentLanguage(), prop2.getContentLanguage());
    assertEquals(prop1.getContentMD5(), prop2.getContentMD5());
    assertEquals(prop1.getContentType(), prop2.getContentType());

    assertEquals("value", copySas.getMetadata().get("Test"));
    assertEquals(copyId, copySas.getCopyState().getCopyId());

    snapshot.delete();
    source.delete();
    copySas.delete();
    container.delete();
}