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

The following examples show how to use com.microsoft.azure.storage.blob.CloudBlockBlob#upload() . 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: AzureStorageUploader.java    From movie-db-java-on-azure with MIT License 8 votes vote down vote up
/**
 * Upload image file to Azure storage with specified name.
 *
 * @param file     image file object
 * @param fileName specified file name
 * @return relative path of the created image blob
 */
public String uploadToAzureStorage(ApplicationContext applicationContext, MultipartFile file, String fileName) {
    String uri = null;

    try {
        CloudStorageAccount storageAccount =
                (CloudStorageAccount) applicationContext.getBean("cloudStorageAccount");
        CloudBlobClient blobClient = storageAccount.createCloudBlobClient();

        setupContainer(blobClient, this.thumbnailImageContainer);
        CloudBlobContainer originalImageContainer = setupContainer(blobClient, this.originalImageContainer);

        if (originalImageContainer != null) {
            CloudBlockBlob blob = originalImageContainer.getBlockBlobReference(fileName);
            blob.upload(file.getInputStream(), file.getSize());

            uri = blob.getUri().getPath();
        }
    } catch (Exception e) {
        e.printStackTrace();
        logger.error("Error uploading image: " + e.getMessage());
    }

    return uri;
}
 
Example 2
Source File: TestBlobService.java    From jframe with Apache License 2.0 7 votes vote down vote up
public void testUploadBlob() {
    try {
        // Define the path to a local file.
        final String filePath = "C:\\myimages\\myimage.jpg";

        // Create or overwrite the "myimage.jpg" blob with contents from a
        // local file.
        // container.getDirectoryReference("").
        CloudBlockBlob blob = container.getBlockBlobReference("myimage.jpg");
        File source = new File(filePath);
        blob.upload(new FileInputStream(source), source.length());
    } catch (Exception e) {
        // Output the stack trace.
        e.printStackTrace();
    }
}
 
Example 3
Source File: AzureStorageService.java    From crate with Apache License 2.0 7 votes vote down vote up
public void writeBlob(String container, String blobName, InputStream inputStream, long blobSize,
                      boolean failIfAlreadyExists)
    throws URISyntaxException, StorageException, IOException {
    LOGGER.trace(() -> new ParameterizedMessage("writeBlob({}, stream, {})", blobName, blobSize));
    final Tuple<CloudBlobClient, Supplier<OperationContext>> client = client();
    final CloudBlobContainer blobContainer = client.v1().getContainerReference(container);
    final CloudBlockBlob blob = blobContainer.getBlockBlobReference(blobName);
    try {
        final AccessCondition accessCondition =
            failIfAlreadyExists ? AccessCondition.generateIfNotExistsCondition() : AccessCondition.generateEmptyCondition();
        blob.upload(inputStream, blobSize, accessCondition, null, client.v2().get());
    } catch (final StorageException se) {
        if (failIfAlreadyExists && se.getHttpStatusCode() == HttpURLConnection.HTTP_CONFLICT &&
            StorageErrorCodeStrings.BLOB_ALREADY_EXISTS.equals(se.getErrorCode())) {
            throw new FileAlreadyExistsException(blobName, null, se.getMessage());
        }
        throw se;
    }
    LOGGER.trace(() -> new ParameterizedMessage("writeBlob({}, stream, {}) - done", blobName, blobSize));
}
 
Example 4
Source File: AzureBackuper.java    From cassandra-backup with Apache License 2.0 5 votes vote down vote up
@Override
public void uploadFile(final long size,
                       final InputStream localFileStream,
                       final RemoteObjectReference object) throws Exception {
    final CloudBlockBlob blob = ((AzureRemoteObjectReference) object).blob;
    blob.upload(localFileStream, size);
}
 
Example 5
Source File: AzureTableStore.java    From data-transfer-project with Apache License 2.0 5 votes vote down vote up
@Override
public void create(UUID jobId, String key, InputStream stream) {
  try {
    CloudBlobContainer reference = blobClient.getContainerReference(BLOB_CONTAINER);
    CloudBlockBlob blob = reference.getBlockBlobReference(createRowKey(jobId, key));
    blob.upload(stream, UNKNOWN_LENGTH);
  } catch (StorageException | URISyntaxException | IOException e) {
    throw new MicrosoftStorageException("Error creating stream for job: " + jobId, e);
  }
}
 
Example 6
Source File: AzureStorageHelper.java    From azure-gradle-plugins with MIT License 5 votes vote down vote up
public static String uploadFileAsBlob(final File fileToUpload, final CloudStorageAccount storageAccount,
                                      final String containerName, final String blobName) throws Exception {
    final CloudBlobContainer blobContainer = getBlobContainer(storageAccount, containerName);
    blobContainer.createIfNotExists(BlobContainerPublicAccessType.BLOB, null, null);

    final CloudBlockBlob blob = blobContainer.getBlockBlobReference(blobName);
    blob.upload(new FileInputStream(fileToUpload), fileToUpload.length());
    return blob.getUri().toString();
}
 
Example 7
Source File: TestWasbUriAndConfiguration.java    From hadoop with Apache License 2.0 5 votes vote down vote up
@Test
public void testConnectUsingSASReadonly() throws Exception {
  // Create the test account with SAS credentials.
  testAccount = AzureBlobStorageTestAccount.create("", EnumSet.of(
      CreateOptions.UseSas, CreateOptions.CreateContainer,
      CreateOptions.Readonly));
  assumeNotNull(testAccount);

  // Create a blob in there
  final String blobKey = "blobForReadonly";
  CloudBlobContainer container = testAccount.getRealContainer();
  CloudBlockBlob blob = container.getBlockBlobReference(blobKey);
  ByteArrayInputStream inputStream = new ByteArrayInputStream(new byte[] { 1,
      2, 3 });
  blob.upload(inputStream, 3);
  inputStream.close();

  // Make sure we can read it from the file system
  Path filePath = new Path("/" + blobKey);
  FileSystem fs = testAccount.getFileSystem();
  assertTrue(fs.exists(filePath));
  byte[] obtained = new byte[3];
  DataInputStream obtainedInputStream = fs.open(filePath);
  obtainedInputStream.readFully(obtained);
  obtainedInputStream.close();
  assertEquals(3, obtained[2]);
}
 
Example 8
Source File: TestWasbUriAndConfiguration.java    From big-c with Apache License 2.0 5 votes vote down vote up
@Test
public void testConnectUsingSASReadonly() throws Exception {
  // Create the test account with SAS credentials.
  testAccount = AzureBlobStorageTestAccount.create("", EnumSet.of(
      CreateOptions.UseSas, CreateOptions.CreateContainer,
      CreateOptions.Readonly));
  assumeNotNull(testAccount);

  // Create a blob in there
  final String blobKey = "blobForReadonly";
  CloudBlobContainer container = testAccount.getRealContainer();
  CloudBlockBlob blob = container.getBlockBlobReference(blobKey);
  ByteArrayInputStream inputStream = new ByteArrayInputStream(new byte[] { 1,
      2, 3 });
  blob.upload(inputStream, 3);
  inputStream.close();

  // Make sure we can read it from the file system
  Path filePath = new Path("/" + blobKey);
  FileSystem fs = testAccount.getFileSystem();
  assertTrue(fs.exists(filePath));
  byte[] obtained = new byte[3];
  DataInputStream obtainedInputStream = fs.open(filePath);
  obtainedInputStream.readFully(obtained);
  obtainedInputStream.close();
  assertEquals(3, obtained[2]);
}
 
Example 9
Source File: AzureStorageBlobService.java    From components with Apache License 2.0 5 votes vote down vote up
public void upload(final String containerName, final String blobName, final InputStream sourceStream, final long length)
        throws StorageException, IOException, URISyntaxException, InvalidKeyException {
    CloudBlobClient cloudBlobClient = connection.getCloudStorageAccount().createCloudBlobClient();
    CloudBlobContainer cloudBlobContainer = cloudBlobClient.getContainerReference(containerName);
    CloudBlockBlob blob = cloudBlobContainer.getBlockBlobReference(blobName);
    blob.upload(sourceStream, length, null, null, AzureStorageUtils.getTalendOperationContext());
}
 
Example 10
Source File: AnalyticsTestHelper.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
public static List<String> CreateLogs(CloudBlobContainer container, StorageService service, int count,
        Calendar start, Granularity granularity) throws URISyntaxException, StorageException, IOException {
    String name;
    List<String> blobs = new ArrayList<String>();
    DateFormat hourFormat = new SimpleDateFormat("yyyy/MM/dd/HH");
    hourFormat.setTimeZone(TimeZone.getTimeZone("GMT"));

    CloudBlockBlob blockBlob;
    name = service.toString().toLowerCase(Locale.US) + "/" + hourFormat.format(start.getTime()) + "00/000001.log";
    blockBlob = container.getBlockBlobReference(name);
    blockBlob.upload(getRandomDataStream(1), 1, null, null, null);
    blobs.add(name);

    for (int i = 1; i < count; i++) {
        if (granularity.equals(Granularity.HOUR)) {
            start.add(GregorianCalendar.HOUR_OF_DAY, 1);
        }
        else if (granularity.equals(Granularity.DAY)) {
            start.add(GregorianCalendar.DAY_OF_MONTH, 1);
        }
        else if (granularity.equals(Granularity.MONTH)) {
            start.add(GregorianCalendar.MONTH, 1);
        }
        else {
            throw new IllegalArgumentException(String.format(Locale.US,
                    "CreateLogs granularity of '{0}' is invalid.", granularity));
        }

        name = service.toString().toLowerCase(Locale.US) + "/" + hourFormat.format(start.getTime())
                + "00/000001.log";
        blockBlob = container.getBlockBlobReference(name);
        blockBlob.upload(getRandomDataStream(1), 1, null, null, null);
        blobs.add(name);
    }

    return blobs;
}
 
Example 11
Source File: AzureBlobBackupUploader.java    From timbuctoo with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void storeBackup(File zipfile) throws BackupUploadException {
  try {
    final CloudBlockBlob blob = container.getBlockBlobReference(blobname);
    blob.upload(new FileInputStream(zipfile), zipfile.length());
  } catch (URISyntaxException | StorageException | IOException e) {
    throw new BackupUploadException(e);
  }
}
 
Example 12
Source File: QueryMetricsAndActivityLogs.java    From azure-libraries-for-java with MIT License 4 votes vote down vote up
private static void addBlobTransactions(String storageConnectionString) throws IOException, URISyntaxException, InvalidKeyException, StorageException {
    // Get the script to upload
    //
    InputStream scriptFileAsStream = QueryMetricsAndActivityLogs
            .class
            .getResourceAsStream("/install_apache.sh");

    // Get the size of the stream
    //
    int fileSize;
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    byte[] buffer = new byte[256];
    int bytesRead;
    while ((bytesRead = scriptFileAsStream.read(buffer)) != -1) {
        outputStream.write(buffer, 0, bytesRead);
    }
    fileSize = outputStream.size();
    outputStream.close();

    // Upload the script file as block blob
    //
    CloudStorageAccount account = CloudStorageAccount.parse(storageConnectionString);
    CloudBlobClient cloudBlobClient = account.createCloudBlobClient();
    CloudBlobContainer container = cloudBlobClient.getContainerReference("scripts");
    container.createIfNotExists();

    ServiceProperties serviceProps = cloudBlobClient.downloadServiceProperties();

    // configure Storage logging and metrics
    LoggingProperties logProps = new LoggingProperties();
    logProps.setLogOperationTypes(EnumSet.of(LoggingOperations.READ, LoggingOperations.WRITE));
    logProps.setRetentionIntervalInDays(2);
    logProps.setVersion("1.0");
    serviceProps.setLogging(logProps);

    MetricsProperties metricProps = new MetricsProperties();
    metricProps.setMetricsLevel(MetricsLevel.SERVICE_AND_API);
    metricProps.setRetentionIntervalInDays(2);
    metricProps.setVersion("1.0");
    serviceProps.setHourMetrics(metricProps);
    serviceProps.setMinuteMetrics(metricProps);

    // Set the default service version to be used for anonymous requests.
    serviceProps.setDefaultServiceVersion("2015-04-05");

    // Set the service properties.
    cloudBlobClient.uploadServiceProperties(serviceProps);

    CloudBlockBlob blob = container.getBlockBlobReference("install_apache.sh");
    blob.upload(scriptFileAsStream, fileSize);

    // give sometime for the infrastructure to process the records and fit into time grain.
    SdkContext.sleep(6 * 60000);
}
 
Example 13
Source File: GenericTests.java    From azure-storage-android with Apache License 2.0 4 votes vote down vote up
public void testReadTimeoutIssue() throws URISyntaxException, StorageException, IOException {
    // part 1
    byte[] buffer = BlobTestHelper.getRandomBuffer(1 * 1024 * 1024);

    // set the maximum execution time
    BlobRequestOptions options = new BlobRequestOptions();
    options.setMaximumExecutionTimeInMs(5000);

    CloudBlobClient blobClient = TestHelper.createCloudBlobClient();
    CloudBlobContainer container = blobClient.getContainerReference(generateRandomContainerName());

    String blobName = "testBlob";
    final CloudBlockBlob blockBlobRef = container.getBlockBlobReference(blobName);
    blockBlobRef.setStreamWriteSizeInBytes(1 * 1024 * 1024);

    ByteArrayInputStream inputStream = new ByteArrayInputStream(buffer);
    BlobOutputStream blobOutputStream = null;

    try {
        container.createIfNotExists();
        blobOutputStream = blockBlobRef.openOutputStream(null, options, null);
        try {
            blobOutputStream.write(inputStream, buffer.length);
        } finally {
            blobOutputStream.close();
        }
        assertTrue(blockBlobRef.exists());
    } finally {
        inputStream.close();
        container.deleteIfExists();
    }

    // part 2
    int length2 = 10 * 1024 * 1024;
    byte[] uploadBuffer2 = BlobTestHelper.getRandomBuffer(length2);

    CloudBlobClient blobClient2 = TestHelper.createCloudBlobClient();
    CloudBlobContainer container2 = blobClient2.getContainerReference(generateRandomContainerName());

    String blobName2 = "testBlob";
    final CloudBlockBlob blockBlobRef2 = container2.getBlockBlobReference(blobName2);

    ByteArrayInputStream inputStream2 = new ByteArrayInputStream(uploadBuffer2);

    try {
        container2.createIfNotExists();

        blockBlobRef2.upload(inputStream2, length2);
    } finally {
        inputStream2.close();
        container2.deleteIfExists();
    }
}
 
Example 14
Source File: MaximumExecutionTimeTests.java    From azure-storage-android with Apache License 2.0 4 votes vote down vote up
@Test
@Category({ DevFabricTests.class, DevStoreTests.class, SlowTests.class })
public void testMaximumExecutionTimeBlobByteArray() throws URISyntaxException, StorageException, IOException {
    int length = 10 * 1024 * 1024;
    byte[] uploadBuffer = BlobTestHelper.getRandomBuffer(length);
    byte[] downloadBuffer = new byte[length];

    // set a delay in sending request
    OperationContext opContext = new OperationContext();
    setDelay(opContext, 2500);

    // set the maximum execution time
    BlobRequestOptions options = new BlobRequestOptions();
    options.setMaximumExecutionTimeInMs(2000);

    CloudBlobClient blobClient = TestHelper.createCloudBlobClient();
    CloudBlobContainer container = blobClient.getContainerReference(generateRandomName("container"));

    String blobName = "testBlob";
    final CloudBlockBlob blockBlobRef = container.getBlockBlobReference(blobName);

    ByteArrayInputStream inputStream = new ByteArrayInputStream(uploadBuffer);

    try {
        container.createIfNotExists();

        blockBlobRef.upload(inputStream, length);
        assertTrue(blockBlobRef.exists());

        try {
            blockBlobRef.downloadToByteArray(downloadBuffer, 0, null, options, opContext);
            fail("Maximum execution time was reached but request did not fail.");
        }
        catch (StorageException e) {
            assertEquals(SR.MAXIMUM_EXECUTION_TIMEOUT_EXCEPTION, e.getCause().getMessage());
        }
    }
    finally {
        inputStream.close();
        container.deleteIfExists();
    }
}
 
Example 15
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();
}
 
Example 16
Source File: AzureFileManager.java    From SEAL-Demo with MIT License 3 votes vote down vote up
/**
 * Upload a file to Azure
 * @param file File to upload
 * @param fileSize Size of file to upload
 * @param containerName Name of the Azure Blob Container
 * @param destinationName File name
 * @throws URISyntaxException if containerName contains incorrect Uri syntax
 * @throws InvalidKeyException if containerName contains an invalid key
 * @throws StorageException if the blob client is unable to get a container reference
 * @throws IOException if there is an error reading the file
 * @throws ExecutionException if the blob client is unable to get a container reference
 * @throws InterruptedException if the blob client is unable to get a container reference
 */
public static void UploadFile(InputStream file, long fileSize, String containerName, String destinationName)
        throws URISyntaxException,
               StorageException,
               IOException,
               ExecutionException,
               InterruptedException {
    CloudBlobContainer container = getContainer(containerName);

    CloudBlockBlob fileBlob = container.getBlockBlobReference(destinationName);
    fileBlob.upload(file, fileSize);
}