Skip to content

Commit

Permalink
Merge pull request #20 from XiaoningLiu/10.1.0
Browse files Browse the repository at this point in the history
10.1.0-preview
  • Loading branch information
vinjiang authored Sep 30, 2018
2 parents 9c79880 + 8f9b21a commit f9deab4
Show file tree
Hide file tree
Showing 50 changed files with 2,680 additions and 2,922 deletions.
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ typings/
/blob/dist/
/blob/typings/
/blob/browser/
/blob/es6dist/
/blob/dist-*/

# Visual Studio Code
.vscode/
.vscode/

statistics.html
3 changes: 3 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"singleQuote": false
}
36 changes: 28 additions & 8 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Contribution Guideline

Hello! Thank you for being interested in contributing to our project!

Please make sure you've followed the instructions provided in the [Azure Projects Contribution Guidelines](http://azure.github.io/guidelines/).
Expand All @@ -8,7 +10,7 @@ The Azure Storage development team uses Visual Studio Code. However, any preferr

### Install

- Node.js valid LTS versions
- Node.js valid LTS versions (>=6.5.0)
- Browsers like Chrome, Edge or Firefox
- Clone the source code from GitHub

Expand All @@ -18,32 +20,50 @@ The Azure Storage development team uses Visual Studio Code. However, any preferr

The only step to configure testing is to set the appropriate environment variables. Create environment variables named "ACCOUNT_NAME", "ACCOUNT_KEY" or "ACCOUNT_SAS". The first two will be used for most requests. The "ACCOUNT_SAS" will only be used for tests in browsers.

You can generate a valid account SAS from Azure portal or tools like Azure Storage Explorer. A SAS starts with "?". And if you are using Windows CMD, you may need quotes to escape special characters like following:

```bash
set "ACCOUNT_SAS=<YOUR_SAS>"
```

#### CORS

You need to set up CORS rules for your storage account if you need to develop for browsers. Go to Azure portal and Azure Storage Explorer, find your storage account, create new CORS rules for blob/queue/file/table service(s).

For example, you can create following CORS settings for debugging. But please customize the settings carefully according to your requirements in production environment.

- Allowed origins: *
- Allowed verbs: DELETE,GET,HEAD,MERGE,POST,OPTIONS,PUT
- Allowed headers: *
- Exposed headers: *
- Maximum age (seconds): 86400

### Building

This project is based on TypeScript. For Node.js, generate commonJS module formats, build with:
This project is based on TypeScript. For Node.js, generate commonJS module formats, build with:

```
```bash
npm run build:cjs
```

Generate JavaScript bundles for browsers:

```
```bash
npm run build:browser
```

### Running

To actually run tests in Node.js:

```
```bash
npm install
npm run test:node
```

Run tests in Browsers. After installed Chrome, the default testing browser:

```
```bash
npm install
npm test:browser
```
Expand All @@ -66,8 +86,8 @@ The following are the minimum requirements for any pull request that must be met
- Clean, well-commented, well-designed code
- Try to limit the number of commits for a feature to 1-2. If you end up having too many we may ask you to squash your changes into fewer commits.

* ChangeLog.md needs to be updated describing the new change
* Thoroughly test your feature
- ChangeLog.md needs to be updated describing the new change
- Thoroughly test your feature

### Branching Policy

Expand Down
195 changes: 133 additions & 62 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ Please note that this version of the SDK is a compete overhaul of the current [A

This SDK is compatible with Node.js and browsers, and validated against LTS Node.js versions (>=6.5) and latest versions of Chrome, Firefox and Edge.

#### Compatible with IE11

You need polyfills to make this library work with IE11. The easiest way is to use [@babel/polyfill](https://babeljs.io/docs/en/babel-polyfill), or [polyfill service](https://polyfill.io/v2/docs/).
Or you can load separate polyfills for missed ES feature(s).
This library depends on following ES6 features which need external polyfills loaded.

* `Promise`
* `String.prototype.startsWith`
* `String.prototype.endsWith`
* `String.prototype.repeat`
* `String.prototype.includes`

#### Differences between Node.js and browsers

There are differences between Node.js and browsers runtime. When getting start with this SDK, pay attention to APIs or classes marked with *"ONLY AVAILABLE IN NODE.JS RUNTIME"* or *"ONLY AVAILABLE IN BROWSERS"*.
Expand All @@ -46,14 +58,6 @@ There are differences between Node.js and browsers runtime. When getting start w
* Parallel uploading and downloading
* `uploadBrowserDataToBlockBlob()`

#### Compatible with IE11

You need polyfills to make this library work with IE11. The easiest way is to using [@babel/polyfill](https://babeljs.io/docs/en/babel-polyfill).
Or you can load separate polyfills for missed ES feature(s).
This library depends on following ES6+ feature(s) which needs external polyfills loaded.

* `Promise`

## Getting Started

### NPM
Expand Down Expand Up @@ -94,6 +98,18 @@ Download latest released JS bundles from links in the [GitHub release page](http

* Blob [https://aka.ms/downloadazurestoragejsblob](https://aka.ms/downloadazurestoragejsblob)

### CORS

You need to set up [Cross-Origin Resource Sharing (CORS)](https://docs.microsoft.com/zh-cn/rest/api/storageservices/cross-origin-resource-sharing--cors--support-for-the-azure-storage-services) rules for your storage account if you need to develop for browsers. Go to Azure portal and Azure Storage Explorer, find your storage account, create new CORS rules for blob/queue/file/table service(s).

For example, you can create following CORS settings for debugging. But please customize the settings carefully according to your requirements in production environment.

* Allowed origins: *
* Allowed verbs: DELETE,GET,HEAD,MERGE,POST,OPTIONS,PUT
* Allowed headers: *
* Exposed headers: *
* Maximum age (seconds): 86400

## SDK Architecture

The Azure Storage SDK for JavaScript provides low-level and high-level APIs. Take Blob SDK as example:
Expand All @@ -105,62 +121,117 @@ The Azure Storage SDK for JavaScript provides low-level and high-level APIs. Tak
## Code Samples

```javascript
const pipeline = StorageURL.newPipeline(
new SharedKeyCredential("account", "accountkey"),
);

// List containers
const serviceURL = new ServiceURL(
"https://account.blob.core.windows.net",
pipeline,
);

let marker;
do {
const listContainersResponse = await serviceURL.listContainersSegment(
Aborter.None,
marker,
const {
Aborter,
BlobURL,
BlockBlobURL,
ContainerURL,
ServiceURL,
StorageURL,
SharedKeyCredential,
AnonymousCredential,
TokenCredential
} = require("@azure/storage-blob");

async function main() {
// Enter your storage account name and shared key
const account = "account";
const accountKey = "accountkey";

// Use SharedKeyCredential with storage account and account key
const sharedKeyCredential = new SharedKeyCredential(account, accountKey);

// Use TokenCredential with OAuth token
const tokenCredential = new TokenCredential("token");
tokenCredential.token = "renewedToken";

// Use AnonymousCredential when url already includes a SAS signature
const anonymousCredential = new AnonymousCredential();

// Use sharedKeyCredential, tokenCredential or tokenCredential to create a pipeline
const pipeline = StorageURL.newPipeline(sharedKeyCredential);

// List containers
const serviceURL = new ServiceURL(
// When using AnonymousCredential, following url should include a valid SAS or support public access
`https://${account}.blob.core.windows.net`,
pipeline
);

let marker;
do {
const listContainersResponse = await serviceURL.listContainersSegment(
Aborter.none,
marker
);

marker = listContainersResponse.marker;
for (const container of listContainersResponse.containerItems) {
console.log(`Container: ${container.name}`);
}
} while (marker);

// Create a container
const containerName = `newcontainer${new Date().getTime()}`;
const containerURL = ContainerURL.fromServiceURL(serviceURL, containerName);

const createContainerResponse = await containerURL.create(Aborter.none);
console.log(
`Create container ${containerName} successfully`,
createContainerResponse.requestId
);

// Create a blob
const content = "hello";
const blobName = "newblob" + new Date().getTime();
const blobURL = BlobURL.fromContainerURL(containerURL, blobName);
const blockBlobURL = BlockBlobURL.fromBlobURL(blobURL);
const uploadBlobResponse = await blockBlobURL.upload(
Aborter.none,
content,
content.length
);
console.log(
`Upload block blob ${blobName} successfully`,
uploadBlobResponse.requestId
);

// List blobs
do {
const listBlobsResponse = await containerURL.listBlobFlatSegment(
Aborter.none,
marker
);

marker = listBlobsResponse.marker;
for (const blob of listBlobsResponse.segment.blobItems) {
console.log(`Blob: ${blob.name}`);
}
} while (marker);

// Get blob content from position 0 to the end
// In Node.js, get downloaded data by accessing downloadBlockBlobResponse.readableStreamBody
// In browsers, get downloaded data by accessing downloadBlockBlobResponse.blobBody
const downloadBlockBlobResponse = await blobURL.download(Aborter.none, 0);
console.log(
"Downloaded blob content",
downloadBlockBlobResponse.readableStreamBody.read(content.length).toString()
);

marker = listContainersResponse.marker;
for (const container of listContainersResponse.containerItems) {
console.log(`Container: ${container.name}`);
}
} while (marker);

// Create a container
const containerName = `newcontainer${new Date().getTime()}`;
const containerURL = ContainerURL.fromServiceURL(serviceURL, containerName);

const createContainerResponse = await containerURL.create(Aborter.None);
console.log(
`Create container ${containerName} successfully`,
createContainerResponse.requestId,
);

// Create a blob
const content = "hello";
const blobName = "newblob" + new Date().getTime();
const blobURL = BlobURL.fromContainerURL(containerURL, blobName);
const blockBlobURL = BlockBlobURL.fromBlobURL(blobURL);
const uploadBlobResponse = await blockBlobURL.upload(
Aborter.None,
content,
content.length,
);
console.log(`Upload block blob ${blobName} successfully`);

// Get blob content in Node.js runtime
const downloadBlockBlobResponse = await blobURL.download(Aborter.None, 0);
console.log(
"Downloaded blob content",
downloadBlockBlobResponse.readableStreamBody.read(content.length).toString(),
);
console.log(`[headers]:${downloadBlockBlobResponse.headers}`);

// Delete container
await containerURL.delete(Aborter.None);
console.log("deleted container");
// Delete container
await containerURL.delete(Aborter.none);

console.log("deleted container");
}

// An async method returns a Promise object, which is compatible with then().catch() coding style.
main()
.then(() => {
console.log("Successfully executed sample.");
})
.catch(err => {
console.log(err.message);
});
```

## More Samples
Expand Down
47 changes: 25 additions & 22 deletions blob/.npmignore
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
!dist/lib/**/*.js
!es6dist/lib/**/*.js
!typings/lib/**/*.d.ts
# browser #
browser/azure-storage.blob.js

# git #
.git*
# dist-esm #
!dist-esm/lib/**/*.js
dist-esm/test
dist-esm/samples

# dist-test #
dist-test/

# Node #
node_modules/

# Test #
test/
dist/test
es6dist/test
typings/test

# Samples #
samples/
dist/samples
es6dist/samples
typings/samples

# Swagger #
swagger/

# lib #
lib/
# typings #
!typings/lib/**/*.d.ts
typings/test
typings/samples

# Browser #
browser/
# git #
.git*

# Test #
test/

# Others #
.vscode/
Expand All @@ -38,10 +38,13 @@ gulpfile.js
.git
.DS_Store
tsconfig.json
tsconfig.es6.json
tslint.json
webpack.config.js
webpack.config.test.js
*.js.map
*.zip
package-lock.json
karma.conf.js
karma.conf.js
temp
gulpfile.js
rollup.config.js
rollup.test.config.js
*.html
Loading

0 comments on commit f9deab4

Please sign in to comment.