AzureStor/README.md

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

2019-07-31 19:22:22 +03:00
# AzureStor <img src="man/figures/logo.png" align="right" width=150 />
2018-05-18 03:48:20 +03:00
2018-12-26 05:41:36 +03:00
[![CRAN](https://www.r-pkg.org/badges/version/AzureStor)](https://cran.r-project.org/package=AzureStor)
![Downloads](https://cranlogs.r-pkg.org/badges/AzureStor)
2020-06-12 13:45:12 +03:00
![Build Status](https://asiadatascience.visualstudio.com/AzureR/_apis/build/status/Azure.AzureStor?branchName=master)
2018-12-26 05:41:36 +03:00
2019-05-23 02:08:09 +03:00
This package implements both an admin- and client-side interface to [Azure Storage Services](https://docs.microsoft.com/en-us/rest/api/storageservices/). The admin interface uses R6 classes and extends the framework provided by [AzureRMR](https://github.com/Azure/AzureRMR). The client interface provides several S3 methods for efficiently managing storage and performing file transfers.
2018-05-18 03:48:20 +03:00
2019-07-30 15:37:39 +03:00
The primary repo for this package is at https://github.com/Azure/AzureStor; please submit issues and PRs there. It is also mirrored at the Cloudyr org at https://github.com/cloudyr/AzureStor. You can install the development version of the package with `devtools::install_github("Azure/AzureStor")`.
2019-07-30 15:37:02 +03:00
2019-01-20 19:14:47 +03:00
## Storage endpoints
The interface for accessing storage is similar across blobs, files and ADLSGen2. You call the `storage_endpoint` function and provide the endpoint URI, along with your authentication credentials. AzureStor will figure out the type of storage from the URI.
AzureStor supports all the different ways you can authenticate with a storage endpoint:
2019-08-05 11:27:28 +03:00
- Blob storage supports authenticating with an access key, shared access signature (SAS), or an Azure Active Directory (AAD) OAuth token;
2019-01-20 19:14:47 +03:00
- File storage supports access key and SAS;
- ADLSgen2 supports access key and AAD token.
2019-02-15 13:11:29 +03:00
In the case of an AAD token, you can also provide an object obtained via `AzureAuth::get_azure_token()`. If you do this, AzureStor can automatically refresh the token for you when it expires.
2019-01-20 19:14:47 +03:00
```r
# various endpoints for an account: blob, file, ADLS2
bl_endp_key <- storage_endpoint("https://mystorage.blob.core.windows.net", key="access_key")
fl_endp_sas <- storage_endpoint("https://mystorage.file.core.windows.net", sas="my_sas")
ad_endp_tok <- storage_endpoint("https://mystorage.dfs.core.windows.net", token="my_token")
# alternative (recommended) way of supplying an AAD token
2019-05-08 22:15:37 +03:00
token <- AzureRMR::get_azure_token("https://storage.azure.com",
2019-01-20 19:14:47 +03:00
tenant="myaadtenant", app="app_id", password="mypassword"))
ad_endp_tok2 <- storage_endpoint("https://mystorage.dfs.core.windows.net", token=token)
```
## Listing, creating and deleting containers
AzureStor provides a rich framework for managing storage. The following generics allow you to manage storage containers:
- `storage_container`: get a storage container (blob container, file share or ADLS filesystem)
- `create_storage_container`
- `delete_storage_container`
- `list_storage_containers`
2019-01-20 19:14:47 +03:00
```r
# example of working with containers (blob storage)
list_storage_containers(bl_endp_key)
cont <- storage_container(bl_endp_key, "mycontainer")
newcont <- create_storage_container(bl_endp_key, "newcontainer")
delete_storage_container(newcont)
2019-01-20 19:14:47 +03:00
```
## Files and blobs
2019-02-15 13:11:29 +03:00
These functions for working with objects within a storage container:
2019-01-20 19:14:47 +03:00
- `list_storage_files`: list files/blobs in a directory (defaults to the root directory)
- `create_storage_dir`/`delete_storage_dir`: create or delete a directory
- `delete_storage_file`: delete a file or blob
2019-12-27 10:55:26 +03:00
- `storage_file_exists`: check that a file or blob exists
- `storage_upload`/`storage_download`: transfer a file to or from a storage container
- `storage_multiupload`/`storage_multidownload`: transfer multiple files in parallel to or from a storage container
2019-11-01 19:46:22 +03:00
- `get_storage_properties`: Get properties for a storage object
- `get_storage_metadata`/`set_storage_metadata`: Get and set user-defined metadata for a storage object
2019-01-20 19:14:47 +03:00
```r
# example of working with files and directories (ADLSgen2)
cont <- storage_container(ad_endp_tok, "myfilesystem")
list_storage_files(cont)
create_storage_dir(cont, "newdir")
2019-11-01 19:46:22 +03:00
storage_download(cont, "/readme.txt")
storage_multiupload(cont, "N:/data/*.*", "newdir") # uploading everything in a directory
```
## Uploading and downloading
AzureStor includes a number of extra features to make transferring files efficient.
2019-01-20 19:14:47 +03:00
2019-10-29 00:37:41 +03:00
### Parallel connections
2019-11-01 19:46:22 +03:00
As noted above, you can transfer multiple files in parallel using the `storage_multiupload/download` functions. These functions utilise a background process pool supplied by AzureRMR to do the transfers in parallel, which usually results in major speedups when transferring multiple small files. The pool is created the first time a parallel file transfer is performed, and persists for the duration of the R session; this means you don't have to wait for the pool to be (re-)created each time.
```r
# uploading/downloading multiple files at once: use a wildcard to specify files to transfer
2019-11-02 12:09:38 +03:00
storage_multiupload(cont, src="N:/logfiles/*.zip")
storage_multidownload(cont, src="/monthly/jan*.*", dest="~/data/january")
2019-10-28 09:29:04 +03:00
# or supply a vector of file specs as the source and destination
src <- c("file1.csv", "file2.csv", "file3.csv")
dest <- file.path("data/", src)
2019-11-01 19:46:22 +03:00
storage_multiupload(cont, src=src, dest=dest)
```
### Transfer to and from connections
You can upload a (single) in-memory R object via a _connection_, and similarly, you can download a file to a connection, or return it as a raw vector. This lets you transfer an object without having to create a temporary file as an intermediate step.
2019-01-20 19:14:47 +03:00
```r
# uploading serialized R objects via connections
json <- jsonlite::toJSON(iris, pretty=TRUE, auto_unbox=TRUE)
con <- textConnection(json)
storage_upload(cont, src=con, dest="iris.json")
2019-01-20 19:14:47 +03:00
rds <- serialize(iris, NULL)
con <- rawConnection(rds)
storage_upload(cont, src=con, dest="iris.rds")
2019-01-20 19:14:47 +03:00
# downloading files into memory: as a raw vector with dest=NULL, and via a connection
rawvec <- storage_download(cont, src="iris.json", dest=NULL)
2019-11-02 12:09:38 +03:00
rawToChar(rawvec)
2019-01-20 19:14:47 +03:00
con <- rawConnection(raw(0), "r+")
storage_download(cont, src="iris.rds", dest=con)
2019-01-20 19:14:47 +03:00
unserialize(con)
```
### Copy from URLs (blob storage only)
The `copy_url_to_storage` function lets you transfer the contents of a URL directly to storage, without having to download it to your local machine first. The `multicopy_url_to_storage` function does the same, but for a vector of URLs. Currently, these only work for blob storage.
```r
# copy from a public URL: Iris data from UCI machine learning repository
copy_url_to_storage(cont,
"https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data",
"iris.csv")
# copying files from another storage account, by appending a SAS to the URL(s)
sas <- "?sv=...."
files <- paste0("https://srcstorage.blob.core.windows.net/container/file", 0:9, ".csv", sas)
multicopy_url_to_storage(cont, files)
```
### Interface to AzCopy
AzureStor includes an interface to [AzCopy](https://docs.microsoft.com/en-us/azure/storage/common/storage-use-azcopy-v10), Microsoft's high-performance commandline utility for copying files to and from storage. To take advantage of this, simply include the argument `use_azcopy=TRUE` on any upload or download function. AzureStor will then call AzCopy to perform the file transfer, rather than using its own internal code. In addition, a `call_azcopy` function is provided to let you use AzCopy for any task.
2019-01-20 19:14:47 +03:00
```r
# use azcopy to download
myfs <- storage_container(ad_endp, "myfilesystem")
storage_download(myfs, "/incoming/bigfile.tar.gz", "/data", use_azcopy=TRUE)
# use azcopy to sync a local and remote dir
call_azcopy("sync", "c:/local/path", "https://mystorage.blob.core.windows.net/mycontainer", "--recursive=true')
2019-01-20 19:14:47 +03:00
```
For more information, see the [AzCopy repo on GitHub](https://github.com/Azure/azure-storage-azcopy).
**Note that AzureStor uses AzCopy version 10. It is incompatible with versions 8.1 and earlier.**
2019-01-20 19:14:47 +03:00
## Admin interface
Finally, AzureStor's admin-side interface allows you to easily create and delete resource accounts, as well as obtain access keys and generate a SAS. Here is a sample workflow:
2018-05-18 03:48:20 +03:00
2018-05-18 04:28:02 +03:00
```r
2018-05-18 03:48:20 +03:00
library(AzureStor)
2018-05-18 03:54:36 +03:00
# authenticate with Resource Manager
2019-07-30 15:37:02 +03:00
az <- AzureRMR::get_azure_login("mytenant")
sub1 <- az$get_subscription("subscription_id")
2019-01-20 19:14:47 +03:00
rg <- sub1$get_resource_group("resgroup")
2018-05-18 03:48:20 +03:00
# get an existing storage account
rdevstor1 <- rg$get_storage("rdevstor1")
rdevstor1
#<Azure resource Microsoft.Storage/storageAccounts/rdevstor1>
# Account type: Storage
# SKU: name=Standard_LRS, tier=Standard
# Endpoints:
# blob: https://rdevstor1.blob.core.windows.net/
# queue: https://rdevstor1.queue.core.windows.net/
# table: https://rdevstor1.table.core.windows.net/
# file: https://rdevstor1.file.core.windows.net/
# ...
2018-05-18 03:48:20 +03:00
# retrieve admin keys
rdevstor1$list_keys()
2018-05-21 11:23:40 +03:00
# create a shared access signature (SAS)
2019-01-20 19:14:47 +03:00
rdevstor1$get_account_sas(permissions="rw")
2018-05-18 03:48:20 +03:00
2019-01-20 19:14:47 +03:00
# obtain an endpoint object for accessing storage (will have the access key included by default)
2018-05-18 03:48:20 +03:00
rdevstor1$get_blob_endpoint()
#Azure blob storage endpoint
#URL: https://rdevstor1.blob.core.windows.net/
#Access key: <hidden>
2019-01-20 19:14:47 +03:00
#Azure Active Directory token: <none supplied>
2018-05-18 03:48:20 +03:00
#Account shared access signature: <none supplied>
2019-01-20 19:14:47 +03:00
#Storage API version: 2018-03-28
2018-05-18 03:48:20 +03:00
# create a new storage account
blobstor2 <- rg$create_storage_account("blobstor2", location="australiaeast", kind="BlobStorage")
# delete it (will ask for confirmation)
blobstor2$delete()
```
2018-10-19 17:45:16 +03:00
---
2019-07-31 19:22:22 +03:00
<p align="center"><a href="https://github.com/Azure/AzureR"><img src="https://github.com/Azure/AzureR/raw/master/images/logo2.png" width=800 /></a></p>