azure-sdk-for-node/README.md

434 строки
14 KiB
Markdown
Исходник Обычный вид История

2014-01-10 22:17:35 +04:00
# Windows Azure SDK for Node.js
2013-10-16 21:04:18 +04:00
2014-04-08 17:28:05 +04:00
[![NPM version](https://badge.fury.io/js/azure.png)](http://badge.fury.io/js/azure) [![Build Status](https://travis-ci.org/Azure/azure-sdk-for-node.png?branch=master)](https://travis-ci.org/Azure/azure-sdk-for-node)
2012-02-24 07:16:26 +04:00
2014-01-10 22:17:35 +04:00
This project provides a Node.js package that makes it easy to consume and manage Windows Azure Services.
# Features
* Storage
* Blob
* Table
* Storage Queue
2012-02-24 07:16:26 +04:00
* Service Bus
2014-01-10 22:17:35 +04:00
* Queue
* Topic
* Notification Hub
2012-04-21 11:37:01 +04:00
* Service Runtime
2014-01-10 22:17:35 +04:00
* [Core management](https://github.com/WindowsAzure/azure-sdk-for-node/blob/master/lib/services/management/README.md)
* [Compute management](https://github.com/WindowsAzure/azure-sdk-for-node/blob/master/lib/services/computeManagement/README.md)
* Virtual Machine
* Cloud Service
* [Web Site management](https://github.com/WindowsAzure/azure-sdk-for-node/blob/master/lib/services/webSiteManagement/README.md)
* [Virtual Network managment](https://github.com/WindowsAzure/azure-sdk-for-node/blob/dev/lib/services/networkManagement/README.md)
* [Storage Account management](https://github.com/WindowsAzure/azure-sdk-for-node/blob/master/lib/services/storageManagement/README.md)
* [SQL Database management](https://github.com/WindowsAzure/azure-sdk-for-node/blob/master/lib/services/sqlManagement/README.md)
* [Service Bus management](https://github.com/WindowsAzure/azure-sdk-for-node/blob/master/lib/services/serviceBusManagement/README.md)
* HDInsight management
2012-04-21 11:37:01 +04:00
2012-02-24 07:16:26 +04:00
# Getting Started
2014-01-10 22:17:35 +04:00
## Install from npm
We provide both fine-grained modules for different Windows Azure services which you can install separately, and an all-up module which contains everything.
2012-02-24 07:19:37 +04:00
2014-01-10 22:17:35 +04:00
**Notice**: we haven't provided fine-grained modules for every supported Windows Azure services yet. This will come soon.
2012-02-24 07:16:26 +04:00
2014-01-10 22:17:35 +04:00
### Install the all-up module
2012-02-24 07:16:26 +04:00
2014-01-10 22:17:35 +04:00
```
npm install azure
```
2012-02-24 07:16:26 +04:00
2014-01-10 22:17:35 +04:00
### Install the fine-grained modules
2012-02-24 07:16:26 +04:00
2014-01-10 22:17:35 +04:00
* Core management: ``npm install azure-mgmt``
* Compute management: ``npm install azure-mgmt-compute``
* Web Site management: ``npm install azure-mgmt-website``
* Virtual Network managment: ``npm install azure-mgmt-vnet``
* Storage Account management: ``npm install azure-mgmt-storage``
* SQL Database management: ``npm install azure-mgmt-sql``
* Service Bus management: ``npm install azure-mgmt-sb``
2012-02-24 07:16:26 +04:00
2014-01-10 22:17:35 +04:00
## Usage
2012-02-24 07:16:26 +04:00
2014-01-10 22:17:35 +04:00
### Table Storage
2012-02-24 07:16:26 +04:00
To ensure a table exists, call **createTableIfNotExists**:
```Javascript
2011-12-10 03:47:08 +04:00
var tableService = azure.createTableService();
tableService.createTableIfNotExists('tasktable', function(error){
if(!error){
// Table exists
}
});
2012-02-24 07:16:26 +04:00
```
A new entity can be added by calling **insertEntity**:
```Javascript
2011-12-10 03:47:08 +04:00
var tableService = azure.createTableService(),
task1 = {
PartitionKey : 'tasksSeattle',
RowKey: '1',
Description: 'Take out the trash',
2014-01-10 22:17:35 +04:00
DueDate: new Date(2011, 12, 14, 12)
2011-12-10 03:47:08 +04:00
};
2014-01-10 22:17:35 +04:00
tableService.insertEntity('tasktable', task1, function(error){
2011-12-10 03:47:08 +04:00
if(!error){
// Entity inserted
}
});
2012-02-24 07:16:26 +04:00
```
The method **queryEntity** can then be used to fetch the entity that was just inserted:
```Javascript
2011-12-10 03:47:08 +04:00
var tableService = azure.createTableService();
tableService.queryEntity('tasktable', 'tasksSeattle', '1', function(error, serverEntity){
if(!error){
// Entity available in serverEntity variable
}
});
2012-02-24 07:16:26 +04:00
```
## Blob Storage
2014-01-10 22:17:35 +04:00
The **createContainerIfNotExists** method can be used to create a
2012-02-24 07:16:26 +04:00
container in which to store a blob:
```Javascript
2011-12-10 03:47:08 +04:00
var blobService = azure.createBlobService();
blobService.createContainerIfNotExists('taskcontainer', {publicAccessLevel : 'blob'}, function(error){
if(!error){
// Container exists and is public
}
});
2012-02-24 07:16:26 +04:00
```
2013-11-03 05:05:30 +04:00
To upload a file (assuming it is called task1-upload.txt and it is placed in the same folder as the script below), the method **createBlob** can be used. This method will return a writable stream which can be writen to, for instance, through piping:
2012-02-24 07:16:26 +04:00
```Javascript
2011-12-10 03:47:08 +04:00
var blobService = azure.createBlobService();
2013-11-03 05:05:30 +04:00
fs.createReadStream('task1-upload.txt').pipe(blobService.createBlob('taskcontainer', 'task1', azure.Constants.BlobConstants.BlobTypes.BLOCK));
2012-02-24 07:16:26 +04:00
```
2013-11-03 05:05:30 +04:00
To download the blob and write it to the file system, a similar **getBlob** method can be used:
2012-02-24 07:16:26 +04:00
```Javascript
2011-12-10 03:47:08 +04:00
var blobService = azure.createBlobService();
2013-11-03 05:05:30 +04:00
blobService.getBlob('taskcontainer', 'task1').pipe(fs.createWriteStream('task1-download.txt'));
2012-02-24 07:16:26 +04:00
```
2013-03-20 19:44:36 +04:00
To create a SAS URL you can use the **getBlobUrl** method. Additionally you can use the **date** helper functions to easily create a SAS that expires at some point relative to the current time.
2013-02-12 07:45:47 +04:00
```Javascript
var blobService = azure.createBlobService();
//create a SAS that expires in an hour
2014-01-10 22:17:35 +04:00
var sharedAccessPolicy = {
2013-02-12 07:45:47 +04:00
AccessPolicy: {
Expiry: azure.date.minutesFromNow(60);
}
};
2013-03-20 19:44:36 +04:00
var sasUrl = blobService.getBlobUrl(containerName, blobName, sharedAccessPolicy);
2013-02-12 08:52:43 +04:00
```
2013-02-12 07:45:47 +04:00
2012-02-24 07:16:26 +04:00
## Storage Queues
The **createQueueIfNotExists** method can be used to ensure a queue exists:
```Javascript
2011-12-10 03:47:08 +04:00
var queueService = azure.createQueueService();
queueService.createQueueIfNotExists('taskqueue', function(error){
if(!error){
// Queue exists
}
});
2012-02-24 07:16:26 +04:00
```
The **createMessage** method can then be called to insert the message into the queue:
```Javascript
2011-12-10 03:47:08 +04:00
var queueService = azure.createQueueService();
2013-04-27 19:27:00 +04:00
queueService.createMessage('taskqueue', 'Hello world!', function(error){
2011-12-10 03:47:08 +04:00
if(!error){
// Message inserted
}
});
2012-02-24 07:16:26 +04:00
```
It is then possible to call the **getMessages** method, process the message and then call **deleteMessage** inside the callback. This two-step process ensures messages don't get lost when they are removed from the queue.
2012-02-24 07:16:26 +04:00
```Javascript
2011-12-10 03:47:08 +04:00
var queueService = azure.createQueueService(),
queueName = 'taskqueue';
queueService.getMessages(queueName, function(error, serverMessages){
if(!error){
// Process the message in less than 30 seconds, the message
2014-01-10 22:17:35 +04:00
// text is available in serverMessages[0].messagetext
2011-12-10 03:47:08 +04:00
queueService.deleteMessage(queueName, serverMessages[0].messageid, serverMessages[0].popreceipt, function(error){
if(!error){
// Message deleted
}
});
}
});
2012-02-24 07:16:26 +04:00
```
2012-04-21 11:37:01 +04:00
## Service Bus Queues
2012-02-24 07:16:26 +04:00
2012-04-21 11:37:01 +04:00
Service Bus Queues are an alternative to Storage Queues that might be useful in scenarios where more advanced messaging features are needed (larger message sizes, message ordering, single-operaiton destructive reads, scheduled delivery) using push-style delivery (using long polling).
2012-02-24 07:16:26 +04:00
The **createQueueIfNotExists** method can be used to ensure a queue exists:
```Javascript
var serviceBusService = azure.createServiceBusService();
serviceBusService.createQueueIfNotExists('taskqueue', function(error){
if(!error){
// Queue exists
}
});
2012-02-24 07:16:26 +04:00
```
The **sendQueueMessage** method can then be called to insert the message into the queue:
```Javascript
var serviceBusService = azure.createServiceBusService();
serviceBusService.sendQueueMessage('taskqueue', 'Hello world!', function(
if(!error){
// Message sent
}
});
2012-02-24 07:16:26 +04:00
```
It is then possible to call the **receiveQueueMessage** method to dequeue the message.
```Javascript
var serviceBusService = azure.createServiceBusService();
serviceBusService.receiveQueueMessage('taskqueue', function(error, serverMessage){
if(!error){
// Process the message
}
});
2012-02-24 07:16:26 +04:00
```
2012-04-21 11:37:01 +04:00
## Service Bus Topics
2012-02-24 07:16:26 +04:00
2012-04-21 11:37:01 +04:00
Service Bus topics are an abstraction on top of Service Bus Queues that make pub/sub scenarios easy to implement.
2012-02-24 07:16:26 +04:00
The **createTopicIfNotExists** method can be used to create a server-side topic:
```Javascript
var serviceBusService = azure.createServiceBusService();
serviceBusService.createTopicIfNotExists('taskdiscussion', function(error){
if(!error){
// Topic exists
}
});
2012-02-24 07:19:37 +04:00
```
2012-02-24 07:16:26 +04:00
The **sendTopicMessage** method can be used to send a message to a topic:
```Javascript
var serviceBusService = azure.createServiceBusService();
serviceBusService.sendTopicMessage('taskdiscussion', 'Hello world!', function(error){
if(!error){
// Message sent
}
});
2012-02-24 07:16:26 +04:00
```
A client can then create a subscription and start consuming messages by calling the **createSubscription** method followed by the **receiveSubscriptionMessage** method. Please note that any messages sent before the subscription is created will not be received.
```Javascript
var serviceBusService = azure.createServiceBusService(),
topic = 'taskdiscussion',
subscription = 'client1';
serviceBusService.createSubscription(topic, subscription, function(error1){
if(!error1){
// Subscription created
serviceBusService.receiveSubscriptionMessage(topic, subscription, function(error2, serverMessage){
if(!error2){
// Process message
}
});
}
});
2012-02-24 07:19:37 +04:00
```
2013-03-20 19:40:36 +04:00
## Notification Hubs
2013-12-03 03:02:55 +04:00
Notification hubs allow you to send notifications to WNS, APNS, GCM, and MPNS receivers.
2013-03-20 19:40:36 +04:00
2013-03-20 20:02:26 +04:00
To create a notification hub, use the method **createNotificationHub**.
2013-03-20 19:40:36 +04:00
```JavaScript
var serviceBusService = azure.createServiceBusService();
serviceBusService.createNotificationHub('hubName', function (err) {
if (!err) {
// Notification hub created successfully
}
});
```
2013-12-03 03:02:55 +04:00
To send notification using native format to the notification hub use the methods of the **wns**, **apns**, **gcm**, **mpns** objects. For a full reference on WNS method templates, check http://msdn.microsoft.com/en-us/library/windows/apps/hh779725.aspx.
To send template (cross-platform) notifications use the send method on the **NotificationHubService** class.
2013-03-20 19:40:36 +04:00
```JavaScript
var notificationHubService = azure.createNotificationHubService('hubName');
2013-12-03 03:02:55 +04:00
// WNS notification
2013-03-20 19:40:36 +04:00
notificationHubService.wns.sendTileSquarePeekImageAndText01(
null,
{
image1src: 'http://foobar.com/dog.jpg',
image1alt: 'A dog',
text1: 'This is a dog',
text2: 'The dog is nice',
text3: 'The dog bites',
text4: 'Beware of dog'
},
function (error) {
if (!error) {
// message sent successfully
}
});
2013-12-03 03:02:55 +04:00
// APNS notification
2013-03-20 19:40:36 +04:00
notificationHubService.apns.send(
null,
{
alert: 'This is my toast message for iOS!',
expiry: expiryDate
},
function (error) {
if (!error) {
// message sent successfully
}
});
2013-06-06 22:59:42 +04:00
2013-12-03 03:02:55 +04:00
// GCM notification
2013-06-06 22:59:42 +04:00
notificationHubService.gcm.send(
null,
{
data: { message: 'Here is a message' }
},
function (error) {
if (!error) {
//message send successfully
}
});
2013-12-03 03:02:55 +04:00
// MPNS notification
notificationHubService.mpns.sendToast(
null,
{
text1: 'A dog',
2014-01-10 22:17:35 +04:00
text2: 'This is a dog'
2013-12-03 03:02:55 +04:00
},
function (error) {
if (!error) {
//message send successfully
}
});
// template notification
notificationHubService.send(
null,
{
message: 'This is my template notification',
2014-01-10 22:17:35 +04:00
goesTo: 'all registrations irrespective of the platform'
2013-12-03 03:02:55 +04:00
},
function (error) {
if (!error) {
//message send successfully
}
});
2013-03-20 19:40:36 +04:00
```
2013-12-03 03:02:55 +04:00
To create registrations (for both native and template notifications), use the creation methods in the **wns**, **apns**, **gcm**, **mpns**. To retrieve, update and delete existing registrations, use the following methods in NotificationHubService: **getRegistration**, **listRegistrations**, **listRegistrationsByTag**, **updateRegistration**, and **deleteRegistration**.
2013-02-12 08:46:02 +04:00
## Service Runtime
The Service Runtime allows you to interact with the machine environment where the current role is running. Please note that these commands will only work if your code is running in a worker role inside the Azure emulator or in the cloud.
2014-01-10 22:17:35 +04:00
The **isAvailable** method lets you determine whether the service runtime endpoint is running on the local machine. It is good practice to enclose any code that
2013-02-12 08:46:02 +04:00
uses service runtime in the isAvailable callback.
```JavaScript
azure.RoleEnvironment.isAvailable(function(error, available) {
if (available) {
// Place your calls to service runtime here
}
});
```
The **getConfigurationSettings** method lets you obtain values from the role's .cscfg file.
```Javascript
azure.RoleEnvironment.getConfigurationSettings(function(error, settings) {
if (!error) {
// You can get the value of setting "setting1" via settings['setting1']
2014-01-10 22:17:35 +04:00
}
2013-02-12 08:46:02 +04:00
});
```
2014-01-10 22:17:35 +04:00
The **getLocalResources** method lets you find the path to defined local storage resources for the current role. For example, the DiagnosticStore
2013-02-12 08:46:02 +04:00
resource which is defined for every role provides a location for runtime diagnostics and logs.
```Javascript
azure.RoleEnvironment.getLocalResources(function(error, resources) {
if(!error){
2014-01-10 22:17:35 +04:00
// You can get the path to the role's diagnostics store via
2013-02-12 08:46:02 +04:00
// resources['DiagnosticStore']['path']
}
});
```
The **getCurrentRoleInstance** method lets you obtain information about endpoints defined for the current role instance:
```JavaScript
azure.RoleEnvironment.getCurrentRoleInstance(function(error, instance) {
if (!error && instance['endpoints']) {
// You can get information about "endpoint1" such as its address and port via
// instance['endpoints']['endpoint1']['address'] and instance['endpoints']['endpoint1']['port']
}
});
```
The **getRoles** method lets you obtain information about endpoints in role instances running on other machines:
```Javascript
azure.RoleEnvironment.getRoles(function(error, roles) {
if(!error){
// You can get information about "instance1" of "role1" via roles['role1']['instance1']
2014-01-10 22:17:35 +04:00
}
2013-02-12 08:46:02 +04:00
});
```
2012-02-24 07:16:26 +04:00
# Need Help?
2014-01-10 22:17:35 +04:00
* [Windows Azure Forums on MSDN and Stack Overflow](http://go.microsoft.com/fwlink/?LinkId=234489)
* IRC channel on freenode: node-azure
2011-12-10 03:47:08 +04:00
2012-02-24 07:16:26 +04:00
# Learn More
2012-11-20 19:36:24 +04:00
2014-01-10 22:17:35 +04:00
* [Windows Azure Node.js Developer Center](http://www.windowsazure.com/en-us/develop/nodejs/)
* [API reference](http://dl.windowsazure.com/nodedocs/)
* [Windows Azure Cross-Platform CLI](http://github.com/windowsazure/azure-sdk-tools-xplat)
2012-11-20 19:36:24 +04:00
2014-01-10 22:17:35 +04:00
# Contribute
2012-11-20 19:36:24 +04:00
2014-01-10 22:17:35 +04:00
* If you would like to become an active contributor to this project please follow the instructions provided in [Windows Azure Projects Contribution Guidelines](http://windowsazure.github.com/guidelines.html).
* If you encounter any bugs with the library please file an issue in the [Issues](https://github.com/WindowsAzure/azure-sdk-for-node/issues) section of the project.