Giter VIP home page Giter VIP logo

azure-sdk-for-java-storage-blob-upload-download's Introduction

page_type languages products description urlFragment
sample
java
azure
azure-storage
How to upload and download blobs from Azure Blob Storage with Java.
upload-download-blobs-java

How to upload and download blobs from Azure Blob Storage with Java

This sample shows how to do the following operations of Storage Blobs with Storage SDK

  • Create a container
  • Upload a blob
  • Enumerate blobs
  • Download a blob
  • Delete a blob
  • Delete a container

Use latest Storage Blob SDK

The Storage Blob SDK package version in this repo is 11.x.x. It's strongly recommended that you use the latest version of the Storage Blob SDK package, please refer to the following examples:

  • BasicExample.java - Examples for common Storage Blob tasks:
    • Create a container
    • Upload a blob
    • Download a blob
    • Enumerate blobs
    • Delete the blob
    • Delete the container

Prerequisites

If you don't have an Azure subscription, create a free account before you begin.

Create a Storage Account using the Azure portal

Step 1 : Create a new general-purpose storage account to use for this tutorial.

  • Go to the Azure Portal and log in using your Azure account.
  • Select New > Storage > Storage account.
  • Select your Subscription.
  • For Resource group, create a new one and give it a unique name.
  • Enter a name for your storage account.
  • Select the Location to use for your Storage Account.
  • Set Account kind to StorageV2(general purpose v2).
  • Set Performance to Standard.
  • Set Replication to Locally-redundant storage (LRS).
  • Set Secure transfer required to Disabled.
  • Check Review + create and click Create to create your Storage Account.

Step 2 : Copy and save keys.

  • After your Storage Account is created. Click on it to open it. Select Settings > Access keys > key1/key2, Select a key and copy the Key to the clipboard, then paste it into text editor for later use.

Set credentials in environment variables

Linux

export AZURE_STORAGE_ACCOUNT="<YourAccountName>"
export AZURE_STORAGE_ACCESS_KEY="<YourAccountKey>"

Windows

setx AZURE_STORAGE_ACCOUNT "<YourAccountName>"
setx AZURE_STORAGE_ACCESS_KEY "<YourAccountKey>"

Run the application

First, clone the repository on your machine:

git clone https://github.com/Azure-Samples/azure-sdk-for-java-storage-blob-upload-download.git

Finally, run the application with the mvn compile exec:java command.

mvn compile exec:java

Resources

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.

When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.

azure-sdk-for-java-storage-blob-upload-download's People

Contributors

jerrypei1997 avatar jongio avatar rikkigibson avatar roygara avatar selvasingh avatar supernova-eng avatar tzhanl avatar v-hongli1 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

azure-sdk-for-java-storage-blob-upload-download's Issues

xception in thread "main" java.lang.IllegalArgumentException: Input byte array has incorrect ending byte at 16 at java.base/java.util.Base64$Decoder.decode0(Base64.java:876) at java.base/java.util.Base64$Decoder.decode(Base64.java:566)

Exception in thread "main" java.lang.IllegalArgumentException: Input byte array has incorrect ending byte at 16
at java.base/java.util.Base64$Decoder.decode0(Base64.java:876)
at java.base/java.util.Base64$Decoder.decode(Base64.java:566)
at com.azure.cosmos.implementation.BaseAuthorizationTokenProvider.createMac(BaseAuthorizationTokenProvider.java:218)
at com.azure.cosmos.implementation.BaseAuthorizationTokenProvider.lambda$reInitializeIfPossible$0(BaseAuthorizationTokenProvider.java:250)
at com.azure.cosmos.implementation.MacPool.(MacPool.java:27)
at com.azure.cosmos.implementation.BaseAuthorizationTokenProvider.reInitializeIfPossible(BaseAuthorizationTokenProvider.java:250)
at com.azure.cosmos.implementation.BaseAuthorizationTokenProvider.(BaseAuthorizationTokenProvider.java:38)
at com.azure.cosmos.implementation.RxDocumentClientImpl.(RxDocumentClientImpl.java:459)
at com.azure.cosmos.implementation.RxDocumentClientImpl.(RxDocumentClientImpl.java:330)
at com.azure.cosmos.implementation.RxDocumentClientImpl.(RxDocumentClientImpl.java:290)
at com.azure.cosmos.implementation.AsyncDocumentClient$Builder.build(AsyncDocumentClient.java:275)
at com.azure.cosmos.CosmosAsyncClient.(CosmosAsyncClient.java:170)
at com.azure.cosmos.CosmosClientBuilder.buildAsyncClient(CosmosClientBuilder.java:1084)
at com.azure.cosmos.CosmosClientBuilder.buildAsyncClient(CosmosClientBuilder.java:1071)

Get the above exception when I try to use and run the below api.

	 CosmosAsyncClient	 client = new CosmosClientBuilder()
			                              .endpoint(endpointUri)
			                              .key(connection_string)
			                             .buildAsyncClient();

public String connection_string = "AccountEndpoint=https://";

Is that conneciton_string needs to be encrypted ? but how it has to be ?

Caused by: java.lang.IllegalArgumentException: Input byte array has wrong 4-byte ending unit

When running the program locally (on my mac), seeing the below error.

String accountName = "acsazurestore";
String accountKey = "DefaultEndpointsProtocol=https;AccountName=xx;AccountKey=xxx";
// Create a ServiceURL to call the Blob service. We will also use this to construct the ContainerURL
SharedKeyCredentials creds = new SharedKeyCredentials(accountName, accountKey);

Caused by: java.lang.IllegalArgumentException: Input byte array has wrong 4-byte ending unit at java.util.Base64$Decoder.decode0(Base64.java:704) at java.util.Base64$Decoder.decode(Base64.java:526) at java.util.Base64$Decoder.decode(Base64.java:549) at com.microsoft.azure.storage.blob.SharedKeyCredentials.<init>(SharedKeyCredentials.java:60) at quickstart.Quickstart.main(Quickstart.java:160)

I had to comment out the createTempFile and downloadFile since running locally.

[bug] Maven dependency issues in the v12 folder

Maven fails to fetch the dependencies for the v12.0.0 components.

Tested it a bit, and these dependencies work:

  <dependencies>
    <dependency>
     <groupId>com.azure</groupId>
     <artifactId>azure-storage-common</artifactId>
     <version>12.13.0</version>
   </dependency>
    <dependency>
     <groupId>com.azure</groupId>
     <artifactId>azure-storage-blob</artifactId>
     <version>12.14.0</version>
   </dependency>
  </dependencies>

The <repository> section is unnecessary, these dependencies exist on maven central.

Tested with:

mvn -version
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: C:\Program Files\Maven\bin\..
Java version: 11.0.7, vendor: Azul Systems, Inc., runtime: C:\Program Files\Zulu\zulu-11
Default locale: de_DE, platform encoding: Cp1252
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"

java -version
openjdk version "11.0.7" 2020-04-14 LTS
OpenJDK Runtime Environment Zulu11.39+15-CA (build 11.0.7+10-LTS)
OpenJDK 64-Bit Server VM Zulu11.39+15-CA (build 11.0.7+10-LTS, mixed mode)

Also temporarily disabled the maven settings.xml to avoid potential trouble with my private repos, but same result.

azure-storage与azure-storage-blob冲突

错误原因:
azure-storage-blob与azure-storage的jar包有冲突,Azure Table Storage和Azure Blob不能在同一个Project中创建,此问题导致获取blobList会导致ListBlobsOptions报java.lang.NoSuchFieldError: NONE错误。
实测10.1.0-10.3.0都包含此问题。

错误信息:
Exception in thread "main" java.lang.NoSuchFieldError: NONE
at com.microsoft.azure.storage.blob.ListBlobsOptions.(ListBlobsOptions.java:36)
at com.microsoft.azure.storage.blob.ListBlobsOptions.(ListBlobsOptions.java:27)
at quickstart.Quickstart.listBlobs(Quickstart.java:67)
at quickstart.Quickstart.main(Quickstart.java:188)

POM配置:

com.microsoft.azure
azure-storage
8.0.0

    <dependency>
        <groupId>com.microsoft.azure</groupId>
        <artifactId>azure-storage-blob</artifactId>
        <version>10.3.0</version>
    </dependency>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.