Ejemplo n.º 1
0
    static async ValueTask <bool> CopyLocalFileToStorageFile(StorageClient client, string localFilePath, string storagePath)
    {
        // TODO (pri 3): make file i/o more efficient
        FileInfo fileInfo = new FileInfo(localFilePath);

        var             createRequest = new CreateFileRequest(storagePath, fileInfo.Length);
        StorageResponse response      = await client.SendRequest(createRequest).ConfigureAwait(false);

        if (response.StatusCode == 201)
        {
            using (var bytes = new FileStream(localFilePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, true))
            {
                var putRequest = new PutRangeRequest(storagePath, bytes);
                response = await client.SendRequest(putRequest).ConfigureAwait(false);

                if (response.StatusCode == 201)
                {
                    return(true);
                }
            }
        }

        Log.TraceEvent(TraceEventType.Error, 0, "Response Status Code {0}", response.StatusCode);
        return(false);
    }
Ejemplo n.º 2
0
        public async Task <List <string> > UploadFile(List <IFormFile> files)
        {
            List <string> responses = new List <string>();

            foreach (var formFile in files)
            {
                DateTime timeStamp = DateTime.Now;
                if (formFile.Length > 0)
                {
                    var FileName = Path.GetFileNameWithoutExtension(formFile.FileName) + '_' + timeStamp.ToString("yyyyMMddHHmmssffff") + Path.GetExtension(formFile.FileName);

                    StorageRequest storageRequest = new StorageRequest()
                    {
                        FolderName = "recursos/",
                        FileName   = FileName,
                        File       = formFile
                    };

                    StorageResponse storageResponse = await StorageService.UploadObject(storageRequest);

                    responses.Add(storageResponse.FileName);
                }
            }
            return(responses);
        }
Ejemplo n.º 3
0
 public async Task <StorageResponse[]> GetDataAsync(params StorageRequest[] requests)
 {
     StorageResponse[] sr = new StorageResponse[requests.Length];
     for (int i = 0; i < requests.Length; i++)
     {
         sr[i] = new StorageResponse(requests[i], data.Variables[requests[i].VariableName].GetData(requests[i].Origin, requests[i].Shape));
     }
     return(sr);
 }
Ejemplo n.º 4
0
    static async ValueTask <bool> CopyStorageFileToLocalFile(StorageClient client, string storagePath, string localFilePath)
    {
        var             request  = new GetFileRequest(storagePath);
        StorageResponse response = await client.SendRequest(request).ConfigureAwait(false);

        if (response.StatusCode != 200)
        {
            Log.TraceEvent(TraceEventType.Error, 0, "Response Status Code {0}", response.StatusCode);
            return(false);
        }

        ulong bytesToRead = response.ContentLength;

        using (var file = new FileStream(localFilePath, FileMode.Create, FileAccess.Write, FileShare.None, 4096, FileOptions.Asynchronous))
        {
            await file.WriteAsync(response.Body, bytesToRead);
        }

        return(true);
    }
Ejemplo n.º 5
0
        public IHttpActionResult Get(string extension)
        {
            Regex rg = new Regex(@"^[a-zA-Z0-9]{1,3}$");

            if (!rg.IsMatch(extension))
            {
                throw new HttpResponseException(System.Net.HttpStatusCode.BadRequest);
            }

            string            connectionString = SettingsHelper.LocalStorageConnectionString;
            var               account          = CloudStorageAccount.Parse(connectionString);
            StorageRepository repo             = new StorageRepository(account);

            //Get the SAS token for the container.  Allow writes for 2 minutes
            var sasToken = repo.GetBlobContainerSASToken();

            //Get the blob so we can get the full path including container name
            var id          = Guid.NewGuid().ToString();
            var newFileName = id + "." + extension;

            string blobURL = repo.GetBlobURI(
                newFileName,
                DAL.Azure.StorageConfig.UserUploadBlobContainerName).ToString();


            //This function determines which storage account the blob will be
            //uploaded to, enabling the future possibility of sharding across
            //multiple storage accounts.
            var client = account.CreateCloudBlobClient();

            var response = new StorageResponse
            {
                ID = id,
                StorageAccountName = client.BaseUri.Authority.Split('.')[0],
                BlobURL            = blobURL,
                BlobSASToken       = sasToken,
                ServerFileName     = newFileName
            };

            return(Ok(response));
        }
Ejemplo n.º 6
0
        public IHttpActionResult Get(string extension)
        {
            Regex rg = new Regex(@"^[a-zA-Z0-9]{1,3}$");
            if(!rg.IsMatch(extension))
            {
                throw new HttpResponseException(System.Net.HttpStatusCode.BadRequest);
            }

            string connectionString = SettingsHelper.LocalStorageConnectionString;
            var account = CloudStorageAccount.Parse(connectionString);
            StorageRepository repo = new StorageRepository(account);

            //Get the SAS token for the container.  Allow writes for 2 minutes
            var sasToken = repo.GetBlobContainerSASToken();

            //Get the blob so we can get the full path including container name
            var id = Guid.NewGuid().ToString();
            var newFileName = id + "." + extension;

            string blobURL = repo.GetBlobURI(
                newFileName, 
                DAL.Azure.StorageConfig.UserUploadBlobContainerName).ToString();


            //This function determines which storage account the blob will be
            //uploaded to, enabling the future possibility of sharding across 
            //multiple storage accounts.
            var client = account.CreateCloudBlobClient();

            var response = new StorageResponse
            {
                ID = id,
                StorageAccountName = client.BaseUri.Authority.Split('.')[0],
                BlobURL = blobURL,                
                BlobSASToken = sasToken,
                ServerFileName = newFileName
            };

            return Ok(response);
        }
        public async Task <StorageResponse> Get(string id, KeyType keyType, DateTime?from = null, DateTime?to = null, string filter = "", string token = "")
        {
            var response  = new StorageResponse();
            var entrys    = new List <StorageEntry>();
            var contToken = _tokenSerializer.DeSerialize(token);
            TableQuerySegment <DynamicTableEntity> storageRequest = await GetData(id, keyType, contToken, from, to, filter);

            response.ContinuationToken = _tokenSerializer.Serialize(storageRequest.ContinuationToken);

            var headings = new List <string>();

            for (int i = 0; i < storageRequest.Results.Count; i++)
            {
                var item = storageRequest.Results[i];
                if (i == 0)
                {
                    foreach (var key in item.Properties.Keys)
                    {
                        headings.Add(key);
                    }
                }
                var entry = new StorageEntry
                {
                    PartitionKey = item.PartitionKey,
                    RowKey       = item.RowKey,
                    TimeStamp    = item.Timestamp
                };
                var values = new List <string>();
                foreach (var key in headings)
                {
                    values.Add(item.Properties.ContainsKey(key) ? GetValue(item.Properties[key]).ToString() : "");
                }
                entry.Values = values;
                entrys.Add(entry);
            }
            response.Headings       = headings;
            response.StorageEntries = entrys;
            return(response);
        }
Ejemplo n.º 8
0
    static async ValueTask <bool> CopyLocalFileToStorageFile(StorageClient client, string localFilePath, string storagePath)
    {
        // TODO (pri 3): make file i/o more efficient
        FileInfo fileInfo = new FileInfo(localFilePath);

        var             createRequest = new CreateFileRequest(storagePath, fileInfo.Length);
        StorageResponse response      = await client.SendRequest(createRequest).ConfigureAwait(false);

        if (response.StatusCode == 201)
        {
            using (var bytes = new FileStream(localFilePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, true))
            {
                long bytesLeft = bytes.Length;
                long index     = 0;
                int  length    = 1024 * 1024 * 4;
                while (bytesLeft > 0)
                {
                    if (bytesLeft < length)
                    {
                        length = (int)bytesLeft;
                    }
                    var putRequest = new PutRangeRequest(storagePath, bytes, index, length);
                    response = await client.SendRequest(putRequest).ConfigureAwait(false);

                    if (response.StatusCode != 201)
                    {
                        Log.TraceEvent(TraceEventType.Error, 0, "Response Status Code {0}", response.StatusCode);
                        return(false);
                    }
                    index     += length;
                    bytesLeft -= length;
                    Debug.Assert(bytesLeft >= 0);
                }
            }
        }

        return(true);
    }