Пример #1
0
        public virtual Uri UploadFile(
            string storageName,
            Uri blobEndpointUri,
            string storageKey,
            string filePath,
            BlobRequestOptions blobRequestOptions)
        {
            StorageCredentials credentials = new StorageCredentials(storageName, storageKey);
            CloudBlobClient client = new CloudBlobClient(blobEndpointUri, credentials);
            string blobName = string.Format(
                CultureInfo.InvariantCulture,
                "{0}_{1}",
                DateTime.UtcNow.ToString("yyyyMMdd_HHmmss", CultureInfo.InvariantCulture),
                Path.GetFileName(filePath));

            CloudBlobContainer container = client.GetContainerReference(ContainerName);
            container.CreateIfNotExists();
            CloudBlockBlob blob = container.GetBlockBlobReference(blobName);

            BlobRequestOptions uploadRequestOption = blobRequestOptions ?? new BlobRequestOptions();

            if (!uploadRequestOption.ServerTimeout.HasValue)
            {
                uploadRequestOption.ServerTimeout = TimeSpan.FromMinutes(300);
            }

            using (FileStream readStream = File.OpenRead(filePath))
            {
                blob.UploadFromStream(readStream, AccessCondition.GenerateEmptyCondition(), uploadRequestOption);
            }

            return new Uri(string.Format(CultureInfo.InvariantCulture, "{0}{1}{2}{3}", client.BaseUri, ContainerName, client.DefaultDelimiter, blobName));
        }
Пример #2
0
        public static CloudBlobContainer GetCloudBlobContainer(string storageAccount, string storageKey, string containerName)
        {
            CloudStorageAccount cloudStorageAccount = new CloudStorageAccount(new StorageCredentials(storageAccount, storageKey), true);
            CloudBlobClient     blobClient          = cloudStorageAccount.CreateCloudBlobClient();

            return(blobClient?.GetContainerReference(containerName));
        }
Пример #3
0
        /// <summary>
        /// Uploads a file to azure store.
        /// </summary>
        /// <param name="storageName">Store which file will be uploaded to</param>
        /// <param name="storageKey">Store access key</param>
        /// <param name="filePath">Path to file which will be uploaded</param>
        /// <param name="blobRequestOptions">The request options for blob uploading.</param>
        /// <returns>Uri which holds locates the uploaded file</returns>
        /// <remarks>The uploaded file name will be guid</remarks>
        public static Uri UploadFile(string storageName, string storageKey, string filePath, BlobRequestOptions blobRequestOptions)
        {
            string baseAddress = General.BlobEndpointUri(storageName);
            var credentials = new StorageCredentialsAccountAndKey(storageName, storageKey);
            var client = new CloudBlobClient(baseAddress, credentials);
            string blobName = Guid.NewGuid().ToString();

            CloudBlobContainer container = client.GetContainerReference(ContainerName);
            container.CreateIfNotExist();
            CloudBlob blob = container.GetBlobReference(blobName);

            using (FileStream readStream = File.OpenRead(filePath))
            {
                blob.UploadFromStream(readStream, blobRequestOptions);
            }

            return new Uri(
                string.Format(
                    CultureInfo.InvariantCulture,
                    "{0}{1}{2}{3}",
                    client.BaseUri,
                    ContainerName,
                    client.DefaultDelimiter,
                    blobName));
        }
Пример #4
0
        /// <summary>
        /// This is the main entry point for your service instance.
        /// </summary>
        /// <param name="cancellationToken">Canceled when Service Fabric needs to shut down this service instance.</param>
        //protected override async Task RunAsync(CancellationToken cancellationToken)
        //{
        //    // TODO: Replace the following sample code with your own logic
        //    //       or remove this RunAsync override if it's not needed in your service.

        //    long iterations = 0;

        //    while (true)
        //    {
        //        cancellationToken.ThrowIfCancellationRequested();

        //        ServiceEventSource.Current.ServiceMessage(this.Context, "Working-{0}", ++iterations);

        //        await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken);
        //    }
        //}



        public async Task <bool> Validate(IlrContext ilrContext, IValidationService validationService)
        {
            using (var logger = ESFA.DC.Logging.LoggerManager.CreateDefaultLogger(ilrContext.CorrelationId.ToString()))
            {
                var startDateTime = DateTime.Now;
                logger.LogInfo($"Validation started for:{ilrContext.Filename} at :{startDateTime}");
                Message message = new Message();
                //try
                //{
                var stopwatch = new Stopwatch();

                stopwatch.Start();

                string xml;

                CloudStorageAccount cloudStorageAccount =
                    CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));



                var cloudStorageAccountElapsed = stopwatch.ElapsedMilliseconds;

                CloudBlobClient cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();

                var cloudBlobClientElapsed = stopwatch.ElapsedMilliseconds;

                CloudBlobContainer cloudBlobContainer =
                    cloudBlobClient.GetContainerReference(ilrContext.ContainerReference);

                var cloudBlobContainerElapsed = stopwatch.ElapsedMilliseconds;

                CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(ilrContext.Filename);

                var cloudBlockBlobElapsed = stopwatch.ElapsedMilliseconds;

                xml = cloudBlockBlob.DownloadText();

                var blob = stopwatch.ElapsedMilliseconds;
                stopwatch.Restart();

                using (var reader = XmlReader.Create(new StringReader(xml)))
                {
                    var serializer = new XmlSerializer(typeof(Message));
                    message = serializer.Deserialize(reader) as Message;
                }

                var deserialize = stopwatch.ElapsedMilliseconds;
                stopwatch.Restart();
                IEnumerable <LearnerValidationError> results;
                var totalLearners = message.Learner.Count();

                if (ilrContext.IsShredAndProcess)
                {
                    // create actors here.
                    results = DivideAndConquer(ilrContext.CorrelationId, message, logger);
                }
                else
                {
                    results = await validationService.Validate(message);
                }

                var validate = stopwatch.ElapsedMilliseconds;
                var endTime  = DateTime.Now;

                var processTimes = new List <string>()
                {
                    string.Format("Start Time : {0}", startDateTime),
                    string.Format("Learners : {0}", totalLearners),
                    string.Format("Errors : {0}", results.Count()),
                    string.Format("Blob Client : {0}", cloudBlobClientElapsed),
                    string.Format("Blob Container : {0}", cloudBlobContainerElapsed),
                    string.Format("Blob Block Blob : {0}", cloudBlockBlobElapsed),
                    string.Format("Blob Download Text : {0}", blob),
                    string.Format("Deserialize ms : {0}", deserialize),
                    string.Format("Validation ms : {0}", validate),
                    string.Format("End Time : {0}", endTime),
                    string.Format("Total Time : {0}", (endTime - startDateTime).TotalMilliseconds),
                };

                stopwatch.Restart();

                //store the results in reliable dictionary
                await SaveResultsInDataService(ilrContext.CorrelationId, processTimes, xml);

                logger.LogInfo("Stateless Validation Results:{@processTimes}", processTimes.ToArray(), "",
                               ilrContext.Filename);

                var saveResultsTime = stopwatch.ElapsedMilliseconds;
                logger.LogInfo($"saved Results to Db in: {saveResultsTime}");

                stopwatch.Restart();

                //send message topic to be picked up by next service

                dynamic data = new { To = _fundingCalcSqlFilterValue };

                var pubMessage =
                    new BrokeredMessage(new MemoryStream(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(data))))
                {
                    ContentType   = "application/json",
                    Label         = data.To,
                    CorrelationId = ilrContext.CorrelationId.ToString(),
                    MessageId     = Guid.NewGuid().ToString(),
                    Properties    =
                    {
                        { "To",       data.To             },
                        { "fileName", ilrContext.Filename }
                    },
                    TimeToLive = TimeSpan.FromMinutes(2)
                };

                await _topicHelper.SendMessage(pubMessage);

                var pushedToTopicTime = stopwatch.ElapsedMilliseconds;
                logger.LogInfo($"pushed message into Topic in: {pushedToTopicTime} , {data}");

                return(true);
            }
        }
Пример #5
0
        /// <summary>
        /// Delete a job and all cloud storage associated with the job.
        /// </summary>
        /// <param name="jobID">ID of the job.</param>
        /// <param name="ct">Cancellation token.</param>
        public async Task DeleteJobAsync(string jobID, CancellationToken ct)
        {
            Guid parsedID = Guid.Parse(jobID);

            // Delete cloud storage associated with the job.
            await storageClient.GetContainerReference(StorageConstants.GetJobOutputContainer(parsedID)).DeleteIfExistsAsync(ct);

            if (ct.IsCancellationRequested)
            {
                return;
            }

            await storageClient.GetContainerReference(StorageConstants.GetJobContainer(parsedID)).DeleteIfExistsAsync(ct);

            if (ct.IsCancellationRequested)
            {
                return;
            }

            await storageClient.GetContainerReference(jobID).DeleteIfExistsAsync(ct);

            if (ct.IsCancellationRequested)
            {
                return;
            }

            // Delete the job.
            await batchClient.JobOperations.DeleteJobAsync(jobID, cancellationToken : ct);
        }
Пример #6
0
        //[HttpPost("registerPerson")]
        public async Task <IActionResult> AddPerson(Books book)
        {
            decimal d        = 1.23M;
            var     bookdata = new Books()
            {
                BookName = "John",
                Price    = d
            };

            await _bookRepo.InsertOneAsync(bookdata);


            //code for uploading blob data to azure
            BlobStorageService objBlobService = new BlobStorageService(_configuration);

            byte[] fileData  = new byte[book.File.Length];
            string mimeType1 = book.File.ContentType;



            book.Author = objBlobService.UploadFileToBlob("ankush.pdf", book.File);



            string accessKey =
                "DefaultEndpointsProtocol=https;AccountName=referraldocuments;AccountKey=ID4sHh6dof/G8x/Qq83WkvhG4H1hYOi9pI1vxpYasXNtXVERREEv2jcZBWOp0dXmv85wEB9lb6gS2hJrCwylqA==;EndpointSuffix=core.windows.net";
            CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(accessKey);
            CloudBlobClient     cloudBlobClient     = cloudStorageAccount.CreateCloudBlobClient();
            CloudBlobContainer  cloudBlobContainer  = cloudBlobClient.GetContainerReference("uploads");
            var          blockBlob = cloudBlobContainer.GetBlobReference("ankush.pdf");
            string       fileName  = "ankush.pdf";
            MemoryStream memStream = new MemoryStream();
            await blockBlob.DownloadToStreamAsync(memStream);


            HttpResponse response = HttpContext.Response;

            //response.ContentType = blockBlob.Properties.ContentType;
            response.ContentType = blockBlob.Properties.ContentType;
            response.Headers.Add("Content-Disposition", "Attachment; filename=" + fileName);
            response.Headers.Add("Content-Length", blockBlob.Properties.Length.ToString());
            return(File(memStream.ToArray(), blockBlob.Properties.ContentType, "ankush.pdf"));


            //CloudBlockBlob blob = cloudBlobContainer.GetBlockBlobReference("ankush.pdf");
            //var exists = blob.ExistsAsync(); // to verify if file exist
            //await blob.FetchAttributesAsync();
            //byte[] dataBytes1;
            //var outputStream = new MemoryStream();

            //using (StreamReader blobfilestream = new StreamReader(await blob.OpenReadAsync()))
            //{
            //    dataBytes1 = blobfilestream.CurrentEncoding.GetBytes(blobfilestream.ReadToEnd());
            //    await blob.DownloadToStreamAsync(outputStream);
            //}

            //Byte[] value = BitConverter.GetBytes(dataBytes1.Length - 1);
            //string mimeType = "application/pdf";
            //Response.Headers.Add("Content-Disposition", "inline; filename=" + "filename.pdf");

            //return File(value, mimeType,"abc.pdf");

            //code for triggering mail using azure grid
            //var apiKey = _configuration.GetSection("SENDGRID_API_KEY").Value;
            //var client = new SendGridClient(apiKey);
            //var from = new EmailAddress("*****@*****.**", "Example User 1");
            //List<EmailAddress> tos = new List<EmailAddress>
            //{
            //    new EmailAddress("*****@*****.**", "Example User 2")
            //};

            //var subject = "Hello world email from Sendgrid ";
            //var htmlContent = "<strong>Hello world with HTML content</strong>";
            //var displayRecipients = false; // set this to true if you want recipients to see each others mail id
            //var msg = MailHelper.CreateSingleEmailToMultipleRecipients(from, tos, subject, "", htmlContent, false);
            //var response = await client.SendEmailAsync(msg);



            //decimal d = 1.23M;
            //var bookdata = new Books()
            //{
            //    BookName = "John",
            //    Price = d
            //};

            //await _bookRepo.InsertOneAsync(bookdata);
        }
        public async Task <UpsertOutcome> UpsertAsync(ResourceWrapper resource, WeakETag weakETag, bool allowCreate, bool keepHistory, CancellationToken cancellationToken)
        {
            await _model.EnsureInitialized();

            int etag = 0;

            if (weakETag != null && !int.TryParse(weakETag.VersionId, out etag))
            {
                throw new ResourceConflictException(weakETag);
            }

            var resourceMetadata = new ResourceMetadata(
                resource.CompartmentIndices,
                resource.SearchIndices?.ToLookup(e => _searchParameterTypeMap.GetSearchValueType(e)),
                resource.LastModifiedClaims);

            DynamicsCrmFhirDataStore crmFhirDataStoreObj = new DynamicsCrmFhirDataStore();

            try
            {
                // Dynamics Crm code
                // write to Cds
                crmFhirDataStoreObj.PutCdsObservationData(resource.RawResource.Data);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return(null);
            }

            string storageConnection = "DefaultEndpointsProtocol=https;AccountName=clusterstudiostorage;AccountKey=0WY56Pft4WN3GIUfjhGGpGMewvtUO55AMULDOiCj/s1IviuEd4kMbHNYi83mgnZm/N6ZGShdpot0i44uDMJgNA==;EndpointSuffix=core.windows.net";
            CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(storageConnection);
            CloudBlobClient     blobClient          = cloudStorageAccount.CreateCloudBlobClient();
            CloudBlobContainer  blobContainer       = blobClient.GetContainerReference("database");

            blobContainer.CreateIfNotExists();
            string errorBlobName      = "CDSData/" + System.DateTime.Now.ToString("yyyy-MM-dd-hh-mm") + ".Json";
            var    exceptionblob      = blobContainer.GetBlockBlobReference(errorBlobName);
            var    cdsObservationData = crmFhirDataStoreObj.GetCdsObservationData();

            exceptionblob.UploadTextAsync(cdsObservationData.ToString()).GetAwaiter().GetResult();

            using (var connection = new SqlConnection(_configuration.ConnectionString))
            {
                await connection.OpenAsync(cancellationToken);

                using (var command = connection.CreateCommand())
                    using (var stream = new RecyclableMemoryStream(_memoryStreamManager))
                        using (var gzipStream = new GZipStream(stream, CompressionMode.Compress))
                            using (var writer = new StreamWriter(gzipStream, ResourceEncoding))
                            {
                                writer.Write(resource.RawResource.Data);
                                writer.Flush();

                                stream.Seek(0, 0);

                                V1.UpsertResource.PopulateCommand(
                                    command,
                                    baseResourceSurrogateId: ResourceSurrogateIdHelper.LastUpdatedToResourceSurrogateId(resource.LastModified.UtcDateTime),
                                    resourceTypeId: _model.GetResourceTypeId(resource.ResourceTypeName),
                                    resourceId: resource.ResourceId,
                                    eTag: weakETag == null ? null : (int?)etag,
                                    allowCreate: allowCreate,
                                    isDeleted: resource.IsDeleted,
                                    keepHistory: keepHistory,
                                    requestMethod: resource.Request.Method,
                                    rawResource: stream,
                                    tableValuedParameters: _upsertResourceTvpGenerator.Generate(resourceMetadata));

                                try
                                {
                                    var newVersion = (int?)await command.ExecuteScalarAsync(cancellationToken);

                                    if (newVersion == null)
                                    {
                                        // indicates a redundant delete
                                        return(null);
                                    }

                                    resource.Version = newVersion.ToString();

                                    return(new UpsertOutcome(resource, newVersion == 1 ? SaveOutcomeType.Created : SaveOutcomeType.Updated));
                                }
                                catch (SqlException e)
                                {
                                    switch (e.Number)
                                    {
                                    case SqlErrorCodes.NotFound:
                                        throw new MethodNotAllowedException(Core.Resources.ResourceCreationNotAllowed);

                                    case SqlErrorCodes.PreconditionFailed:
                                        throw new ResourceConflictException(weakETag);

                                    default:
                                        _logger.LogError(e, "Error from SQL database on upsert");
                                        throw;
                                    }
                                }
                            }
            }
        }
Пример #8
0
 public void CreateContainer()
 {
     this.BlobContainer = _blobClient.GetContainerReference(_catalog);
     this.BlobContainer.CreateIfNotExists();
 }
Пример #9
0
        static void Main(string[] args)
        {
            CloudBlobClient myBlobClient = storageAccount.CreateCloudBlobClient();

            myBlobClient.SingleBlobUploadThresholdInBytes = 1024 * 1024;
            CloudBlobContainer container = myBlobClient.GetContainerReference("adokontajnerneki");
            //container.CreateIfNotExists();
            CloudBlockBlob myBlob    = container.GetBlockBlobReference("cfx.zip");
            var            blockSize = 256 * 1024;

            myBlob.StreamWriteSizeInBytes = blockSize;
            var  fileName      = @"D:\cfx.zip";
            long bytesToUpload = (new FileInfo(fileName)).Length;
            long fileSize      = bytesToUpload;

            if (bytesToUpload < blockSize)
            {
                CancellationToken ca = new CancellationToken();
                var ado = myBlob.UploadFromFileAsync(fileName, FileMode.Open, ca);
                Console.WriteLine(ado.Status); //Does Not Help Much
                ado.ContinueWith(t =>
                {
                    Console.WriteLine("Status = " + t.Status);
                    Console.WriteLine("It is over"); //this is working OK
                });
            }
            else
            {
                List <string> blockIds      = new List <string>();
                int           index         = 1;
                long          startPosition = 0;
                long          bytesUploaded = 0;
                do
                {
                    var bytesToRead  = Math.Min(blockSize, bytesToUpload);
                    var blobContents = new byte[bytesToRead];
                    using (FileStream fs = new FileStream(fileName, FileMode.Open))
                    {
                        fs.Position = startPosition;
                        fs.Read(blobContents, 0, (int)bytesToRead);
                    }
                    ManualResetEvent mre = new ManualResetEvent(false);
                    var blockId          = Convert.ToBase64String(Encoding.UTF8.GetBytes(index.ToString("d6")));
                    Console.WriteLine("Now uploading block # " + index.ToString("d6"));
                    blockIds.Add(blockId);
                    var ado = myBlob.PutBlockAsync(blockId, new MemoryStream(blobContents), null);
                    ado.ContinueWith(t =>
                    {
                        bytesUploaded += bytesToRead;
                        bytesToUpload -= bytesToRead;
                        startPosition += bytesToRead;
                        index++;
                        double percentComplete = (double)bytesUploaded / (double)fileSize;
                        Console.WriteLine("Percent complete = " + percentComplete.ToString("P"));
                        mre.Set();
                    });
                    mre.WaitOne();
                }while (bytesToUpload > 0);
                Console.WriteLine("Now committing block list");
                var pbl = myBlob.PutBlockListAsync(blockIds);
                pbl.ContinueWith(t =>
                {
                    Console.WriteLine("Blob uploaded completely.");
                });
            }
            Console.ReadKey();
        }
Пример #10
0
        public bool ConvertPictureToJpeg(string pictureId, string containerName, string directoryPath)
        {
            try
            {
                // Retrieve a reference to a container
                CloudBlobContainer sourceContainer = blobClient.GetContainerReference(containerName);
                CloudBlobDirectory sourceDir       = sourceContainer.GetDirectoryReference(directoryPath);

                // Retrieve reference to the stored preview blob
                CloudBlob sourceBlob = sourceDir.GetBlobReference(pictureId);

                System.Drawing.Image imgStored;
                using (MemoryStream ms = new MemoryStream())
                {
                    sourceBlob.DownloadToStream(ms);
                    imgStored = System.Drawing.Image.FromStream(ms);
                }

                if (imgStored != null)
                {
                    UploadPictureToBlobStorage(imgStored, pictureId, containerName, directoryPath, 540, 540, 140, 140, true, false, 80);
                }

                return(true);
            }
            catch
            {
                return(false);
            }
        }
Пример #11
0
        public async Task <ActionResult> UploadAsync()
        {
            var storageConnectionString = _configuration.GetValue <string>("StorageConnectionString");
            var storageAccount          = CloudStorageAccount.Parse(storageConnectionString);

            var computerVisionEndpoint = _configuration.GetValue <string>("COMPUTER_VISION_ENDPOINT");
            var computerVisionKey      = _configuration.GetValue <string>("COMPUTER_VISION_SUBSCRIPTION_KEY");

            _blobClient    = storageAccount.CreateCloudBlobClient();
            _blobContainer = _blobClient.GetContainerReference(_blobContainerName);
            await _blobContainer.CreateIfNotExistsAsync();

            try
            {
                var request = await HttpContext.Request.ReadFormAsync();

                Console.Write(request.Files[0]);

                if (request.Files == null)
                {
                    return(BadRequest("Could not upload image"));
                }
                var files = request.Files;
                if (files.Count == 0)
                {
                    return(BadRequest("No file was selected"));
                }

                //upload:
                var blob = _blobContainer.GetBlockBlobReference(files[0].FileName);
                using (var stream = files[0].OpenReadStream())
                {
                    await blob.UploadFromStreamAsync(stream);
                }

                var imageUrl = blob.Uri.ToString();

                //analize:
                ComputerVisionClient client = Authenticate(computerVisionEndpoint, computerVisionKey);

                List <VisualFeatureTypes> features = new List <VisualFeatureTypes>()
                {
                    VisualFeatureTypes.Categories, VisualFeatureTypes.Description,
                    VisualFeatureTypes.Faces, VisualFeatureTypes.ImageType,
                    VisualFeatureTypes.Tags, VisualFeatureTypes.Adult,
                    VisualFeatureTypes.Color, VisualFeatureTypes.Brands,
                    VisualFeatureTypes.Objects
                };

                ImageAnalysis results = await client.AnalyzeImageAsync(imageUrl, features);

                if (FindPersons(results.Objects) || FindFaces(results.Faces))
                {
                    return(Ok("People found!"));
                }

                return(Ok("No one found"));
            }
            catch (Exception ex)
            {
                ViewData["message"] = ex.Message;
                ViewData["trace"]   = ex.StackTrace;
                return(View("Error"));
            }
        }
        /// <summary>
        /// Build process step chaing base on process type configuration
        /// </summary>
        /// <param name="processTypeId"></param>
        /// <returns></returns>
        private List <StepHandler> BuildChain(string processTypeId)
        {
            StepHandler prevStep = null;

            List <StepHandler> auxSteps = new List <StepHandler>();
            string             jsonTxt;

            try
            {
                jsonTxt = ReadConfigOrDefault(processTypeId + ".ChainConfig");
                if (string.IsNullOrEmpty(jsonTxt))
                {
                    throw new Exception(processTypeId + " Not Found, check ButlerConfiguration Table");
                }
            }
            catch (Exception X)
            {
                throw new Exception("[Error at BuildChain] Process " + X.Message);
            }

            //Sensible config manually
            List <stepTypeInfo> StepList = Newtonsoft.Json.JsonConvert.DeserializeObject <List <stepTypeInfo> >(jsonTxt);

            foreach (stepTypeInfo item in StepList)
            {
                //Build the chain
                //1. is the Assembly in bin?
                if (!File.Exists(item.AssemblyName))
                {
                    //try to download from storage
                    try
                    {
                        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(myProcessConfigConn);
                        CloudBlobClient     blobClient     = storageAccount.CreateCloudBlobClient();
                        CloudBlobContainer  container      = blobClient.GetContainerReference("mediabutlerbin");

                        //TODO: fix this is DLL exis and is it on use
                        foreach (IListBlobItem dll in container.ListBlobs(null, false))
                        {
                            Uri            myUri     = dll.Uri;
                            int            seg       = myUri.Segments.Length - 1;
                            string         name      = myUri.Segments[seg];
                            CloudBlockBlob blockBlob = container.GetBlockBlobReference(name);
                            using (var fileStream = System.IO.File.OpenWrite(@".\" + name))
                            {
                                blockBlob.DownloadToStream(fileStream);
                            }
                        }
                        if (!File.Exists(item.AssemblyName))
                        {
                            throw new Exception(item.AssemblyName + " don't exist");
                        }
                    }
                    catch (Exception X)
                    {
                        string txt = string.Format("[{0}] Error BuildChain  Assembly {1} error: {2}", this.GetType().FullName, item.AssemblyName, X.Message);

                        Trace.TraceError(txt);
                        throw X;
                    }
                }

                StepHandler obj = (StepHandler)Activator.CreateComInstanceFrom(item.AssemblyName, item.TypeName).Unwrap();
                if ((item.ConfigKey != null) && (item.ConfigKey != ""))
                {
                    //LOAD STRING CONFIGURATION FOR CONFIG TABLE

                    obj.StepConfiguration = this.ReadConfigOrDefault(item.ConfigKey + ".StepConfig");
                }
                auxSteps.Add(obj);

                if (prevStep != null)
                {
                    prevStep.SetSuccessor(obj);
                }
                prevStep = obj;
            }
            return(auxSteps);
        }
Пример #13
0
        private static void RunApplication(ConfigWrapper config)
        {
            client = CreateMediaServicesClient(config);
            // Set the polling interval for long running operations to 2 seconds.
            // The default value is 30 seconds for the .NET client SDK
            client.LongRunningOperationRetryTimeout = 2;

            // Connect to Storage.
            CloudStorageAccount storageAccount  = CloudStorageAccount.Parse(config.StorageConnectionString);
            CloudBlobClient     cloudBlobClient = storageAccount.CreateCloudBlobClient();

            StartEndpointIfNotRunning(config);

            // Get a list of all of the locators and enumerate through them a page at a time.
            IPage <StreamingLocator> firstPage   = client.StreamingLocators.List(config.ResourceGroup, config.AccountName);
            IPage <StreamingLocator> currentPage = firstPage;

            do
            {
                bool always = false;
                foreach (StreamingLocator locator in currentPage)
                {
                    // Get the asset associated with the locator.
                    Asset asset = client.Assets.Get(config.ResourceGroup, config.AccountName, locator.AssetName);

                    // Get the Storage continer associated with the asset.
                    CloudBlobContainer storageContainer = cloudBlobClient.GetContainerReference(asset.Container);

                    // Get a manifest file list from the Storage container.
                    List <string> fileList = GetFilesListFromStorage(storageContainer);

                    string ismcFileName        = fileList.Where(a => a.ToLower().Contains(".ismc")).FirstOrDefault();
                    string ismManifestFileName = fileList.Where(a => a.ToLower().EndsWith(".ism")).FirstOrDefault();
                    // If there is no .ism then there's no reason to continue.  If there's no .ismc we need to add it.
                    if (ismManifestFileName != null && ismcFileName == null)
                    {
                        Console.WriteLine("Asset {0} does not have an ISMC file.", asset.Name);
                        if (!always)
                        {
                            Console.WriteLine("Add the ISMC?  (y)es, (n)o, (a)lways, (q)uit");
                            ConsoleKeyInfo response     = Console.ReadKey();
                            string         responseChar = response.Key.ToString();

                            if (responseChar.Equals("N"))
                            {
                                continue;
                            }
                            if (responseChar.Equals("A"))
                            {
                                always = true;
                            }
                            else if (!(responseChar.Equals("Y")))
                            {
                                break; // At this point anything other than a 'yes' should quit the loop/application.
                            }
                        }

                        string streamingUrl = GetStreamingUrlsAndDrmType(client, config.ResourceGroup, config.AccountName, locator.Name);
                        // We should only have two items in the list.  First is the Smooth Streaming URL, and second is the DRM scheme
                        if (streamingUrl.Length == 0)
                        {
                            // error state, skip this asset.  We shouldn't ever be here.
                            continue;
                        }

                        string ismcContentXml = SendManifestRequest(new Uri(streamingUrl));
                        if (ismcContentXml.Length == 0)
                        {
                            //error state, skip this asset
                            continue;
                        }
                        if (ismcContentXml.IndexOf("<Protection>") > 0)
                        {
                            Console.WriteLine("Content is encrypted. Removing the protection header from the client manifest.");
                            //remove DRM from the ISCM manifest
                            ismcContentXml = Xml.RemoveXmlNode(ismcContentXml);
                        }
                        string         newIsmcFileName = ismManifestFileName.Substring(0, ismManifestFileName.IndexOf(".")) + ".ismc";
                        CloudBlockBlob ismcBlob        = WriteStringToBlob(ismcContentXml, newIsmcFileName, storageContainer);

                        // Download the ISM so that we can modify it to include the ISMC file link.
                        string ismXmlContent = GetFileXmlFromStorage(storageContainer, ismManifestFileName);
                        ismXmlContent = Xml.AddIsmcToIsm(ismXmlContent, newIsmcFileName);
                        WriteStringToBlob(ismXmlContent, ismManifestFileName, storageContainer);
                        // update the ism to point to the ismc (download, modify, delete original, upload new)
                    }
                }
                // Continue on to the next page of locators.
                try
                {
                    currentPage = client.StreamingLocators.ListNext(currentPage.NextPageLink);
                }
                catch (Exception)
                {
                    // we'll get here at the end of the page when the page is empty.  This is okay.
                }
            } while (currentPage.NextPageLink != null);
        }
        private Uri UploadFile(string storageName, Uri blobEndpointUri, string storageKey, BlobUploadParameters parameters)
        {
            var    credentials = new StorageCredentials(storageName, storageKey);
            var    client      = new CloudBlobClient(blobEndpointUri, credentials);
            string blobName    = parameters.FileRemoteName;

            if (string.IsNullOrEmpty(blobName))
            {
                blobName = string.Format(
                    CultureInfo.InvariantCulture,
                    "{0}_{1}",
                    DateTime.UtcNow.ToString("yyyyMMdd_HHmmss", CultureInfo.InvariantCulture),
                    Path.GetFileName(parameters.FileLocalPath));
            }

            CloudBlobContainer container = client.GetContainerReference(parameters.ContainerName);
            var wasCreated = container.CreateIfNotExists();

            if (wasCreated && parameters.ContainerPublic)
            {
                container.SetPermissions(new BlobContainerPermissions
                {
                    PublicAccess = BlobContainerPublicAccessType.Blob
                });
            }

            CloudBlockBlob blob = container.GetBlockBlobReference(blobName);

            if (blob.Exists())
            {
                if (parameters.OverrideIfExists)
                {
                    blob.DeleteIfExists();
                }
                else
                {
                    throw new ArgumentException(string.Format(CultureInfo.CurrentCulture,
                                                              Commands.Common.Properties.Resources.BlobAlreadyExistsInTheAccount, blobName));
                }
            }

            using (FileStream readStream = File.OpenRead(parameters.FileLocalPath))
            {
                blob.UploadFromStream(readStream, AccessCondition.GenerateEmptyCondition(), parameters.BlobRequestOptions);
            }

            blob = container.GetBlockBlobReference(blobName);

            string sasContainerToken = string.Empty;

            if (!parameters.ContainerPublic)
            {
                //Set the expiry time and permissions for the blob.
                //Start time is specified as a few minutes in the past, to mitigate clock skew.
                //The shared access signature will be valid immediately.
                SharedAccessBlobPolicy sasConstraints = new SharedAccessBlobPolicy();
                sasConstraints.SharedAccessStartTime  = DateTime.UtcNow.AddMinutes(-5);
                sasConstraints.SharedAccessExpiryTime = DateTime.UtcNow.AddHours(parameters.SasTokenDurationInHours);
                sasConstraints.Permissions            = SharedAccessBlobPermissions.Read;

                //Generate the shared access signature on the blob, setting the constraints directly on the signature.
                sasContainerToken = blob.GetSharedAccessSignature(sasConstraints);
            }

            string fullUrl = client.BaseUri + parameters.ContainerName + client.DefaultDelimiter + blobName + sasContainerToken;

            return(new Uri(fullUrl));
        }
Пример #15
0
        public static async Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");
            string responseMessage;

            string blobName = req.Query["blobName"];

            string  requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data        = JsonConvert.DeserializeObject(requestBody);

            blobName = blobName ?? data?.blobName;

            string sourceStorage = Environment.GetEnvironmentVariable("NewImageSourceStorage");

            CloudStorageAccount sourceStorageAccount = CloudStorageAccount.Parse(sourceStorage);

            string             imageContainerName = Environment.GetEnvironmentVariable("ImageContainerName");
            CloudBlobClient    imageBlobClient    = sourceStorageAccount.CreateCloudBlobClient();
            CloudBlobContainer imageContainer     = imageBlobClient.GetContainerReference(imageContainerName);
            CloudBlockBlob     imageBlob          = imageContainer.GetBlockBlobReference(blobName);

            ImageMetadata imageData = new ImageMetadata()
            {
                timestamp        = DateTime.Now,
                uploadedFileName = blobName,
                id = Path.GetFileNameWithoutExtension(blobName)
            };

            using (MemoryStream blobMemStream = new MemoryStream())
            {
                await imageBlob.DownloadToStreamAsync(blobMemStream);


                byte[] byteData = blobMemStream.ToArray();

                log.LogInformation("Image Byte Array:" + byteData);

                var client = new HttpClient();

                // Request headers - replace this example key with your valid Prediction-Key.

                client.DefaultRequestHeaders.Add("Prediction-Key", Environment.GetEnvironmentVariable("CustomVisionPredictionKey"));

                // Prediction URL - replace this example URL with your valid Prediction URL.

                string rootUrl   = Environment.GetEnvironmentVariable("CustomVisionRootUrl");
                string iteration = Environment.GetEnvironmentVariable("CustomVisionIteration");
                string url       = rootUrl + iteration + "/image";

                HttpResponseMessage response;

                // Request body. Try this sample with a locally stored image.

                using (var content = new ByteArrayContent(byteData))
                {
                    content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                    response = await client.PostAsync(url, content);

                    string responseBody = await response.Content.ReadAsStringAsync();

                    imageData = ProcessCustomVisionResults(responseBody, imageData);

                    Console.WriteLine(responseBody);
                }

                if (imageData.isValidatedIssue)
                {
                    log.LogInformation("Uploaded Image has been identified as an issue");

                    string             metaContainerName = Environment.GetEnvironmentVariable("ImageMetadataContainer");
                    CloudBlobClient    metaBlobClient    = sourceStorageAccount.CreateCloudBlobClient();
                    CloudBlobContainer metaContainer     = metaBlobClient.GetContainerReference(metaContainerName);
                    await metaContainer.CreateIfNotExistsAsync();

                    string         newMetaJson = System.Text.Json.JsonSerializer.Serialize <ImageMetadata>(imageData);
                    CloudBlockBlob newMetaBlob = metaContainer.GetBlockBlobReference(imageData.id + ".json");
                    await newMetaBlob.UploadTextAsync(newMetaJson);

                    responseMessage = imageData.id;
                }
                else
                {
                    log.LogInformation("Uploaded Image was not identified as an Issue. Removing image from upload container...");
                    await imageBlob.DeleteIfExistsAsync();

                    responseMessage = "-1";
                }
            }

            return(new OkObjectResult(responseMessage));
        }
Пример #16
0
        /// <summary>
        /// Provides an asynchronous version of the Main method, allowing for the awaiting of async method calls within.
        /// </summary>
        /// <returns>A <see cref = "System.Threading.Tasks.Task"/> object that represents the asynchronous operation.</returns>
        static async Task Main(string[] args)
        {
            // Read the environment variables to allow the app to connect to the Azure Batch and Azure Storage accounts
            batchAccountUrl    = Environment.GetEnvironmentVariable(envVarBatchURI);
            batchAccountName   = Environment.GetEnvironmentVariable(envVarBatchName);
            batchAccountKey    = Environment.GetEnvironmentVariable(envVarKey);
            storageAccountName = Environment.GetEnvironmentVariable(envVarStorage);
            storageAccountKey  = Environment.GetEnvironmentVariable(envVarStorageKey);

            // Show the user the accounts they are attaching to
            Console.WriteLine("BATCH URL: {0}, Name: {1}, Key: {2}", batchAccountUrl, batchAccountName, batchAccountKey);
            Console.WriteLine("Storage Name: {0}, Key: {1}", storageAccountName, storageAccountKey);

            // Construct the Storage account connection string
            string storageConnectionString = String.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", storageAccountName, storageAccountKey);

            // Retrieve the storage account
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(storageConnectionString);

            // Create the blob client, for use in obtaining references to blob storage containers
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            // Use the blob client to create the containers in blob storage
            const string inputContainerName  = "input";
            const string outputContainerName = "output";

            await CreateContainerIfNotExistAsync(blobClient, inputContainerName);
            await CreateContainerIfNotExistAsync(blobClient, outputContainerName);

            // RESOURCE FILE SETUP
            // Add *.mp4 files into the \<solutiondir>\InputFiles folder.
            string        inputPath      = Path.Combine(Environment.CurrentDirectory, "InputFiles");
            List <string> inputFilePaths = new List <string>(Directory.GetFileSystemEntries(inputPath, "*.mp4", SearchOption.TopDirectoryOnly));

            // Upload data files.
            // Upload the data files using UploadResourceFilesToContainer(). This data will be
            // processed by each of the tasks that are executed on the compute nodes within the pool.
            List <ResourceFile> inputFiles = await UploadFilesToContainerAsync(blobClient, inputContainerName, inputFilePaths);

            // Obtain a shared access signature that provides write access to the output container to which
            // the tasks will upload their output.
            string outputContainerSasUrl = GetContainerSasUrl(blobClient, outputContainerName, SharedAccessBlobPermissions.Write);

            // The batch client requires a BatchSharedKeyCredentials object to open a connection
            var sharedKeyCredentials = new BatchSharedKeyCredentials(batchAccountUrl, batchAccountName, batchAccountKey);

            using (BatchClient batchClient = BatchClient.Open(sharedKeyCredentials))
            {
                // Create the Batch pool, which contains the compute nodes that execute the tasks.
                await CreateBatchPoolAsync(batchClient, PoolId);

                // Create the job that runs the tasks.
                await CreateJobAsync(batchClient, JobId, PoolId);

                // Create a collection of tasks and add them to the Batch job.
                // Provide a shared access signature for the tasks so that they can upload their output
                // to the Storage container.
                List <CloudTask> runningTasks = await AddTasksAsync(batchClient, JobId, inputFiles, outputContainerSasUrl);

                // Monitor task success or failure, specifying a maximum amount of time to wait for
                // the tasks to complete.
                await MonitorTasksAsync(batchClient, JobId, TimeSpan.FromMinutes(30));

                // Delete input container in storage
                Console.WriteLine("Deleting container [{0}]...", inputContainerName);
                CloudBlobContainer container = blobClient.GetContainerReference(inputContainerName);
                await container.DeleteIfExistsAsync();

                // Clean up the job (if the user so chooses)
                Console.WriteLine();
                Console.Write("Delete job? [yes] no: ");
                string response = Console.ReadLine().ToLower();
                if (response != "n" && response != "no")
                {
                    Console.WriteLine("Deleting job ...");
                    await batchClient.JobOperations.DeleteJobAsync(JobId);

                    Console.WriteLine("Job deleted.");
                }

                // Clean up the pool (if the user so chooses - do not delete the pool of new batches of videos are ready to process)
                Console.Write("Delete pool? [yes] no: ");
                response = Console.ReadLine().ToLower();
                if (response != "n" && response != "no")
                {
                    Console.WriteLine("Deleting pool ...");
                    await batchClient.PoolOperations.DeletePoolAsync(PoolId);

                    Console.WriteLine("Pool deleted.");
                }
            }
        }
Пример #17
0
        public async Task <List <ImageLists> > UploadImages(List <Stream> strmLists, List <string> lstContntTypes)
        {
            string myContainerName = "Test007";
            string assetID         = CreateBLOBContainer(myContainerName);

            assetID = assetID.Replace("nb:cid:UUID:", "asset-");
            List <ImageLists>   retCollection  = new List <ImageLists>();
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(myAzureStorageConSetting);
            CloudBlobClient     blobClient     = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer  container      = blobClient.GetContainerReference(assetID);

            container.SetPermissions(
                new BlobContainerPermissions {
                PublicAccess = BlobContainerPublicAccessType.Blob
            });

            if (strmLists != null)
            {
                for (int i = 0; i < strmLists.Count; i++)
                {
                    string strExtension = string.Empty;
                    if (lstContntTypes[i] == "image/gif")
                    {
                        strExtension = ".gif";
                    }
                    else if (lstContntTypes[i] == "image/jpeg")
                    {
                        strExtension = ".jpeg";
                    }
                    else if (lstContntTypes[i] == "image/jpg")
                    {
                        strExtension = ".jpg";
                    }
                    else if (lstContntTypes[i] == "image/png")
                    {
                        strExtension = ".png";
                    }
                    ImageLists     img       = new ImageLists();
                    string         imgGUID   = Guid.NewGuid().ToString();
                    CloudBlockBlob blockBlob = container.GetBlockBlobReference(string.Concat(imgGUID, strExtension));
                    await blockBlob.UploadFromStreamAsync(strmLists[i]);

                    img.ImageID   = new Guid(imgGUID);
                    img.Title     = string.Concat(imgGUID, strExtension);
                    img.ImageSize = strmLists[i].Length;
                    img.AssetID   = assetID;
                    retCollection.Add(img);

                    CloudBlockBlob blockblobthumb = container.GetBlockBlobReference(string.Concat(imgGUID, "_thumb", strExtension));
                    Stream         strmThumb      = ResizeImage(strmLists[i]);
                    using (strmThumb)
                    {
                        await blockblobthumb.UploadFromStreamAsync(strmThumb);

                        img           = new ImageLists();
                        img.ImageID   = new Guid(imgGUID);
                        img.Title     = string.Concat(imgGUID, "_thumb", strExtension);
                        img.ImageSize = strmThumb.Length;
                        img.AssetID   = assetID;
                        retCollection.Add(img);
                    }
                }
            }
            return(retCollection);
        }
Пример #18
0
        public async Task <ActionResult> Upload(HttpPostedFileBase file)
        {
            if (file != null && file.ContentLength > 0)
            {
                // Make sure the user selected an image file
                if (!file.ContentType.StartsWith("image"))
                {
                    TempData["Message"] = "Only image files may be uploaded";
                }
                else
                {
                    try
                    {
                        // Save the original image in the "photos" container
                        CloudStorageAccount account   = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"]);
                        CloudBlobClient     client    = account.CreateCloudBlobClient();
                        CloudBlobContainer  container = client.GetContainerReference("photos");
                        CloudBlockBlob      photo     = container.GetBlockBlobReference(Path.GetFileName(file.FileName));
                        await photo.UploadFromStreamAsync(file.InputStream);

                        // Generate a thumbnail and save it in the "thumbnails" container
                        using (var outputStream = new MemoryStream())
                        {
                            file.InputStream.Seek(0L, SeekOrigin.Begin);
                            var settings = new ResizeSettings {
                                MaxWidth = 192
                            };
                            ImageBuilder.Current.Build(file.InputStream, outputStream, settings);
                            outputStream.Seek(0L, SeekOrigin.Begin);
                            container = client.GetContainerReference("thumbnails");
                            CloudBlockBlob thumbnail = container.GetBlockBlobReference(Path.GetFileName(file.FileName));
                            await thumbnail.UploadFromStreamAsync(outputStream);
                        }

                        // Submit the image to Azure's Computer Vision API
                        ComputerVisionClient vision = new ComputerVisionClient(
                            new ApiKeyServiceClientCredentials(ConfigurationManager.AppSettings["SubscriptionKey"]),
                            new System.Net.Http.DelegatingHandler[] { });
                        vision.Endpoint = ConfigurationManager.AppSettings["VisionEndpoint"];

                        VisualFeatureTypes[] features = new VisualFeatureTypes[] { VisualFeatureTypes.Description };
                        var result = await vision.AnalyzeImageAsync(photo.Uri.ToString(), features);

                        // Record the image description and tags in blob metadata
                        photo.Metadata.Add("Caption", result.Description.Captions[0].Text);

                        for (int i = 0; i < result.Description.Tags.Count; i++)
                        {
                            string key = String.Format("Tag{0}", i);
                            photo.Metadata.Add(key, result.Description.Tags[i]);
                        }

                        await photo.SetMetadataAsync();
                    }
                    catch (Exception ex)
                    {
                        // In case something goes wrong
                        TempData["Message"] = ex.Message;
                    }
                }
            }

            return(RedirectToAction("Index"));
        }
        public async Task <string> FileUpload(long id, IFormFile[] files)
        {
            var claimsIdentity = User.Identity as ClaimsIdentity;
            var userId         = Convert.ToInt64(claimsIdentity.Claims.FirstOrDefault(claim => claim.Type == "Id").Value);
            var user           = _multiSourcePlaylistRepository.GetUser(userId);
            var filePath       = Path.Combine(
                "uploads",
                user.FileFolder);
            var uploads = Path.Combine(
                _environment.ContentRootPath,
                filePath);

            //string url = UriHelper.GetDisplayUrl(Request);//http://localhost:8080/api/fileupload/1
            //var urlParts = url.Split(new[] { "api/fileupload" }, StringSplitOptions.None);
            //var baseUrl = urlParts[0];
            var playlist = _multiSourcePlaylistRepository.GetPlaylist(id);

            var          allTracks = _multiSourcePlaylistRepository.GetAllTracks();
            int          lastOrder = 0;
            List <Track> temp      = new List <Track>();

            if (allTracks != null)
            {
                temp = allTracks.Where(y => y.Playlist.Id == id).ToList();
            }
            if (temp != null && temp.Any())
            {
                lastOrder = temp.OrderByDescending(x => x.Order).FirstOrDefault().Order + 1;
            }
            //@string.Format("{0}://{1}{2}{3}", Context.Request.Scheme, Context.Request.Host, Context.Request.Path, Context.Request.QueryString)
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
                _configuration["Production:StorageConnectionString"]);
            CloudBlobClient    blobClient = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer container  = blobClient.GetContainerReference(user.FileFolder);

            // Ensure that the share exists.
            if (await container.ExistsAsync())
            {
                int bytesToMegaBytes = 1048576;
                var totalSize        = await isThereDiscSpaceInAzure(container);

                if ((totalSize) / bytesToMegaBytes > 10000)
                {
                    return("NO_DISC_SPACE");
                }
            }
            else
            {
                return("User container doesn't exists.");
            }
            foreach (var file in files)
            {
                try {
                    var            filename = file.FileName;
                    var            fullpath = Path.Combine(uploads, filename);
                    CloudBlockBlob blob     = container.GetBlockBlobReference(filename);
                    if (!await blob.ExistsAsync())
                    {
                        if (file.Length > 0)
                        {
                            using (var fileStream = file.OpenReadStream())
                            {
                                await blob.UploadFromStreamAsync(fileStream);
                            }
                        }
                    }
                    using (var fileStream = new FileStream(fullpath, FileMode.Create))
                    {
                        await file.CopyToAsync(fileStream);

                        fileStream.Flush();
                        fileStream.Dispose();
                    }

                    Track fileTrack = new Track();
                    fileTrack.Address  = file.FileName;
                    fileTrack.Playlist = playlist;
                    fileTrack.Type     = 5;
                    fileTrack.Order    = lastOrder;
                    fileTrack.Name     = getTrackName(fullpath);//hanki bändi ja kappale mp3 tiedoston metasta
                    _multiSourcePlaylistRepository.PostTrack(fileTrack);
                    ++lastOrder;
                    System.IO.File.Delete(fullpath);
                } catch (Exception ex) {
                    return(ex.Message);
                }
            }
            return("File was Uploaded");
        }
Пример #20
0
        public async Task <ResourceWrapper> GetAsync(ResourceKey key, CancellationToken cancellationToken)
        {
            await _model.EnsureInitialized();

            string storageConnection = "DefaultEndpointsProtocol=https;AccountName=clusterstudiostorage;AccountKey=0WY56Pft4WN3GIUfjhGGpGMewvtUO55AMULDOiCj/s1IviuEd4kMbHNYi83mgnZm/N6ZGShdpot0i44uDMJgNA==;EndpointSuffix=core.windows.net";
            CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(storageConnection);
            CloudBlobClient     blobClient          = cloudStorageAccount.CreateCloudBlobClient();
            CloudBlobContainer  blobContainer       = blobClient.GetContainerReference("database");

            blobContainer.CreateIfNotExists();
            string errorBlobName = "CDSData/" + System.DateTime.Now.ToString("yyyy-MM-dd-hh-mm") + ".Json";
            var    exceptionblob = blobContainer.GetBlockBlobReference(errorBlobName);

            if (datastore == "cds")
            {
                DynamicsCrmFhirDataStore crmFhirDataStoreObj = new DynamicsCrmFhirDataStore();
                var cdsObservationData = crmFhirDataStoreObj.GetCdsObservationData();

                exceptionblob.UploadTextAsync(cdsObservationData.ToString()).GetAwaiter().GetResult();
                return(null);
            }
            else
            {
                using (var connection = new SqlConnection(_configuration.ConnectionString))
                {
                    await connection.OpenAsync(cancellationToken);

                    int?requestedVersion = null;
                    if (!string.IsNullOrEmpty(key.VersionId))
                    {
                        if (!int.TryParse(key.VersionId, out var parsedVersion))
                        {
                            return(null);
                        }

                        requestedVersion = parsedVersion;
                    }

                    using (SqlCommand command = connection.CreateCommand())
                    {
                        V1.ReadResource.PopulateCommand(
                            command,
                            resourceTypeId: _model.GetResourceTypeId(key.ResourceType),
                            resourceId: key.Id,
                            version: requestedVersion);

                        using (SqlDataReader sqlDataReader = await command.ExecuteReaderAsync(CommandBehavior.SequentialAccess, cancellationToken))
                        {
                            if (!sqlDataReader.Read())
                            {
                                return(null);
                            }

                            var resourceTable = V1.Resource;

                            (long resourceSurrogateId, int version, bool isDeleted, bool isHistory, Stream rawResourceStream) = sqlDataReader.ReadRow(
                                resourceTable.ResourceSurrogateId,
                                resourceTable.Version,
                                resourceTable.IsDeleted,
                                resourceTable.IsHistory,
                                resourceTable.RawResource);

                            string rawResource;

                            using (rawResourceStream)
                                using (var gzipStream = new GZipStream(rawResourceStream, CompressionMode.Decompress))
                                    using (var reader = new StreamReader(gzipStream, ResourceEncoding))
                                    {
                                        rawResource = await reader.ReadToEndAsync();
                                    }

                            // exceptionblob.UploadTextAsync(rawResource + Environment.NewLine + cdsObservationData.ToString()).GetAwaiter().GetResult();

                            return(new ResourceWrapper(
                                       key.Id,
                                       version.ToString(CultureInfo.InvariantCulture),
                                       key.ResourceType,
                                       new RawResource(rawResource, FhirResourceFormat.Json),
                                       null,
                                       new DateTimeOffset(ResourceSurrogateIdHelper.ResourceSurrogateIdToLastUpdated(resourceSurrogateId), TimeSpan.Zero),
                                       isDeleted,
                                       searchIndices: null,
                                       compartmentIndices: null,
                                       lastModifiedClaims: null)
                            {
                                IsHistory = isHistory,
                            });
                        }
                    }
                }
            }
        }
Пример #21
0
 public void EnsureContainer()
 {
     _blobContainer = _blobClient.GetContainerReference(_containerName);
     _blobContainer.CreateIfNotExists();
 }
        public void ServiceHost_ClientInputRecieved(object sender, ClientInputMessage e)
        {
            Trace.WriteLine("ClientInputRecieved Recieved User Id : " + e.idUsuario, "Warning");
            try
            {
                /*Crear Blob desde un Stream*/
                // Se obtiene la cuenta de almacenamiento
                storageAccount = CloudStorageAccount.Parse(
                    CloudConfigurationManager.GetSetting("StorageConnectionString"));

                // Se crea el cliente de blobs
                blobClient = storageAccount.CreateCloudBlobClient();

                // Obtencion del container
                container = blobClient.GetContainerReference(VariablesConfiguracion.containerName);

                Int64 subid = 1;

                String mustacheTemplateStr = File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + "/App_Data/mustacheCloud.txt");

                //Template para generar los MDL
                FormatCompiler compiler     = new FormatCompiler();
                Generator      generatorMDL = compiler.Compile(mustacheTemplateStr);

                //Obtener parametros del cliente
                SimulacionParameters parametros = JsonConvert.DeserializeObject <SimulacionParameters>(e.message);

                //Barrido paramétrico
                //Especificar número de parámetros, n
                Int32 n = parametros.reacciones.Count(r => r.rate != null);
                if (n > 0)
                {
                    //Inicializo vector de inicios, fines y steps
                    LinkedList <Double> inicios = new LinkedList <Double>();
                    LinkedList <Double> fines   = new LinkedList <Double>();
                    LinkedList <Double> steps   = new LinkedList <Double>();
                    foreach (var reaccion in parametros.reacciones.FindAll(r => r.rate != null))
                    {
                        var inicio = reaccion.rate.rateInicio;
                        var fin    = reaccion.rate.rateFin;
                        var step   = reaccion.rate.rateStep;
                        inicios.AddLast(inicio);
                        fines.AddLast(fin);
                        steps.AddLast(step);
                    }

                    var iniciosArray = inicios.ToArray();
                    var finesArray   = fines.ToArray();
                    var stepsArray   = steps.ToArray();


                    //Defino vector lógico L para hacer barrido selectivo
                    Int32[] vectorLogico = new Int32[n];
                    for (int i = 0; i < n; i++)
                    {
                        //vectorLogico[i] = i < 5 ? 1 : 100;
                        vectorLogico[i] = 1000;
                    }

                    // Inicialización del vector j
                    Double[] j = new Double[n];
                    for (var k = 0; k < n; k++)
                    {
                        if (k == vectorLogico[k])
                        {
                            iniciosArray[k] += stepsArray[k];
                        }
                        j[k] = iniciosArray[k];
                    }

                    LinkedList <Double[]> jotas = new LinkedList <double[]>();
                    //Barrido parametrico
                    Int32 z = n - 1;
                    while (z >= 0)
                    {
                        if ((j[z] - finesArray[z]) * stepsArray[z] > 0)
                        {
                            j[z] = iniciosArray[z];
                            z--;
                        }
                        else
                        {
                            jotas.AddLast((double[])j.Clone()); //Para ver las combinaciones que creo
                            var    auxId  = subid;
                            Thread thread = new Thread(() => CrearMDL(e, auxId, (double[])j.Clone(), parametros, generatorMDL));
                            thread.Start();
                            //CrearMDL(e, subid, (double[])j.Clone(), parametros, generatorMDL);
                            z = n - 1;
                            subid++;
                        }
                        if (z >= 0)
                        {
                            j[z] += stepsArray[z];
                            if (z == vectorLogico[z])
                            {
                                j[z] += stepsArray[z];
                            }
                        }
                    }
                }
                else
                {
                    List <Double> valores = new List <double>();
                    CrearMDL(e, 0, valores.ToArray(), parametros, generatorMDL);
                }
            }
            catch (Exception ex)
            {
                Trace.TraceError(ex.Message);
                throw;
            }
        }
Пример #23
0
 private CloudBlobContainer GetSoundsContainer()
 {
     //returns the container where the sound files are stored
     MakeContainerAndQueue();
     return(blobClient.GetContainerReference("sounds"));
 }
Пример #24
0
        public async Task <ActionResult> UploadImage(IFormFile file, string username)
        {
            Console.WriteLine("--> ML:: Receiving file");

            if (file == null || file.Length == 0 || !CheckIfImageFile(file))
            {
                return(this.BadRequest());
            }

            ClientDM clientDM = await ARDirect.Instance.Query <ClientDM>().Where("username={0}", username).LoadSingleAsync();

            if (clientDM == null)
            {
                return(this.BadRequest());
            }

            try
            {
                byte[] fileBytes;
                var    ms = new MemoryStream();
                file.CopyTo(ms);
                fileBytes = ms.ToArray();

                string storageConnectionString = "DefaultEndpointsProtocol=https;AccountName=paywall;AccountKey=hDfRSqohMj3NdKjtJhYn2zAnxR7eAZ3dQidDviGnHDPBLEZwfp1ptbPFfvBdjfEqsfmZEboXOyd4s9wUJZDcfA==;EndpointSuffix=core.windows.net";
                CloudStorageAccount storageacc = CloudStorageAccount.Parse(storageConnectionString);
                CloudBlobClient     client     = storageacc.CreateCloudBlobClient();
                CloudBlobContainer  cont       = client.GetContainerReference("aco");

                await cont.SetPermissionsAsync(new BlobContainerPermissions
                {
                    PublicAccess = BlobContainerPublicAccessType.Blob
                });

                string extension = "";
                switch (ImageUploadHelper.GetImageFormat(fileBytes))
                {
                case ImageUploadHelper.ImageFormat.jpeg:
                    extension = ".jpg";
                    break;

                case ImageUploadHelper.ImageFormat.png:
                    extension = ".png";
                    break;

                default:
                    return(this.BadRequest());
                }

                Stream         inputStream = ms;
                string         ImageName   = "profile_" + Guid.NewGuid().ToString().Replace("-", string.Empty) + extension;
                CloudBlockBlob cblob       = cont.GetBlockBlobReference(ImageName);
                await cblob.UploadFromStreamAsync(inputStream);

                clientDM.profilePic = cblob.Uri.AbsoluteUri;
                await clientDM.UpdateAsync();

                return(this.Ok());
            }
            catch (Exception e)
            {
                return(this.BadRequest());
            }
        }
        /// <summary>
        /// Functional Cases : for New-AzureStorageContainer
        /// 1. Create a list of new blob containers (Positive 2)
        /// 2. Create a list of containers that some of them already exist (Negative 4)
        ///
        /// Functional Cases : for Get-AzureStorageContainer
        /// 3.	Get a list of blob containers by using wildcards in the name (Positive 2)
        ///
        /// Functional Cases : for Remove-AzureStorageContainer
        /// 4.	Remove a list of existing blob containers by using pipeline (Positive 6)
        /// </summary>
        internal void ContainerListOperations(Agent agent)
        {
            string PREFIX = Utility.GenNameString("uniqueprefix-") + "-";

            string[] CONTAINER_NAMES = new string[] { Utility.GenNameString(PREFIX), Utility.GenNameString(PREFIX), Utility.GenNameString(PREFIX) };

            // PART_EXISTING_NAMES differs only the last element with CONTAINER_NAMES
            string[] PARTLY_EXISTING_NAMES = new string[CONTAINER_NAMES.Length];
            Array.Copy(CONTAINER_NAMES, PARTLY_EXISTING_NAMES, CONTAINER_NAMES.Length - 1);
            PARTLY_EXISTING_NAMES[CONTAINER_NAMES.Length - 1] = Utility.GenNameString(PREFIX);

            string[] MERGED_NAMES = CONTAINER_NAMES.Union(PARTLY_EXISTING_NAMES).ToArray();
            Array.Sort(MERGED_NAMES);

            // Generate the comparison data
            Collection <Dictionary <string, object> > comp = new Collection <Dictionary <string, object> >();

            foreach (string name in MERGED_NAMES)
            {
                comp.Add(Utility.GenComparisonData(StorageObjectType.Container, name));
            }

            CloudBlobClient blobClient = StorageAccount.CreateCloudBlobClient();

            // Check if all the above containers have been removed
            foreach (string name in MERGED_NAMES)
            {
                CloudBlobContainer container = blobClient.GetContainerReference(name);
                container.DeleteIfExists();
            }

            //--------------1. New operation--------------
            Test.Assert(agent.NewAzureStorageContainer(CONTAINER_NAMES), Utility.GenComparisonData("NewAzureStorageContainer", true));
            // Verification for returned values
            Test.Assert(agent.Output.Count == CONTAINER_NAMES.Count(), "3 row returned : {0}", agent.Output.Count);

            // Check if all the above containers have been created
            foreach (string name in CONTAINER_NAMES)
            {
                CloudBlobContainer container = blobClient.GetContainerReference(name);
                Test.Assert(container.Exists(), "container {0} should exist", name);
            }

            try
            {
                //--------------2. New operation--------------
                Test.Assert(!agent.NewAzureStorageContainer(CONTAINER_NAMES), Utility.GenComparisonData("NewAzureStorageContainer", false));
                // Verification for returned values
                Test.Assert(agent.Output.Count == 0, "0 row returned : {0}", agent.Output.Count);
                int i = 0;
                foreach (string name in CONTAINER_NAMES)
                {
                    Test.Assert(agent.ErrorMessages[i].Equals(String.Format("Container '{0}' already exists.", name)), agent.ErrorMessages[i]);
                    ++i;
                }

                //--------------3. New operation--------------
                Test.Assert(!agent.NewAzureStorageContainer(PARTLY_EXISTING_NAMES), Utility.GenComparisonData("NewAzureStorageContainer", false));
                // Verification for returned values
                Test.Assert(agent.Output.Count == 1, "1 row returned : {0}", agent.Output.Count);

                // Check if all the above containers have been created
                foreach (string name in CONTAINER_NAMES)
                {
                    CloudBlobContainer container = blobClient.GetContainerReference(name);
                    Test.Assert(container.Exists(), "container {0} should exist", name);
                }


                //--------------4. Get operation--------------
                // use wildcards
                Test.Assert(agent.GetAzureStorageContainer("*" + PREFIX + "*"), Utility.GenComparisonData("GetAzureStorageContainer", true));
                // Verification for returned values
                agent.OutputValidation(StorageAccount.CreateCloudBlobClient().ListContainers(PREFIX, ContainerListingDetails.All));

                // use Prefix parameter
                Test.Assert(agent.GetAzureStorageContainerByPrefix(PREFIX), Utility.GenComparisonData("GetAzureStorageContainerByPrefix", true));
                // Verification for returned values
                agent.OutputValidation(StorageAccount.CreateCloudBlobClient().ListContainers(PREFIX, ContainerListingDetails.All));
            }
            finally { }

            //--------------5. Remove operation--------------
            Test.Assert(agent.RemoveAzureStorageContainer(MERGED_NAMES), Utility.GenComparisonData("RemoveAzureStorageContainer", true));
            // Check if all the above containers have been removed
            foreach (string name in CONTAINER_NAMES)
            {
                CloudBlobContainer container = blobClient.GetContainerReference(name);
                Test.Assert(!container.Exists(), "container {0} should not exist", name);
            }
        }
        private CloudBlockBlob GetBlob(string resourceName)
        {
            var container = _cloudBlobClient.GetContainerReference(_containerName);

            return(container.GetBlockBlobReference($"{_basePath}{resourceName}"));
        }
        // IMAGE UPLOAD TO BLOB
        protected void btnUpload_Click(object sender, EventArgs e)
        {
            var blobName = imageUpload.FileName;

            // LIMIT FILE SIZE + FILE TYPE
            if (imageUpload.PostedFile.ContentLength < maxImageSize &&
                (imageUpload.PostedFile.ContentType == "image/x-png" ||
                imageUpload.PostedFile.ContentType == "image/pjpeg" ||
                imageUpload.PostedFile.ContentType == "image/jpeg" ||
                imageUpload.PostedFile.ContentType == "image/bmp" ||
                imageUpload.PostedFile.ContentType == "image/png" ||
                imageUpload.PostedFile.ContentType == "image/gif")
                ) {

                // APP SETTING TRUE/FALSE
                if (Convert.ToBoolean(ConfigurationManager.AppSettings["useazureblob"].ToString()))
                {
                    // AZURE BLOB ACCESSKEY + CONTAINER NAME
                    string accesskey = ConfigurationManager.AppSettings["azurekey"].ToString();
                    string containerName = "dodamimg".ToLower();


                    // IF NO CONTAINER, CREATE IT!
                    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(accesskey);

                    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

                    CloudBlobContainer container = blobClient.GetContainerReference(containerName);

                    container.CreateIfNotExists();

                    container.SetPermissions(
                        new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });


                    // UPLOAD...

                    CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobName);

                    blockBlob.Properties.ContentType = imageUpload.PostedFile.ContentType;
                    blockBlob.UploadFromStream(imageUpload.FileContent);

                    this.testimg.ImageUrl = $"https://dodamblob.blob.core.windows.net/dodamimg/{blobName}";
                    //this.

                    this.imgurl.Text= $"https://dodamblob.blob.core.windows.net/dodamimg/{blobName}";
                }

                //IF APPSETTING IS FALSE, UPLOAD TO LOCAL..
                else
                {

                }
            }

            // FILETYPE CHECK
            else if(imageUpload.PostedFile.ContentLength < maxImageSize &&
                (imageUpload.PostedFile.ContentType != "image/x-png" ||
                imageUpload.PostedFile.ContentType != "image/pjpeg" ||
                imageUpload.PostedFile.ContentType != "image/jpeg" ||
                imageUpload.PostedFile.ContentType != "image/bmp" ||
                imageUpload.PostedFile.ContentType != "image/png" ||
                imageUpload.PostedFile.ContentType != "image/gif"))
            {
                ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('Please upload image ');</script>");
            }

            else if(imageUpload.PostedFile.ContentLength >= maxImageSize &&
                (imageUpload.PostedFile.ContentType == "image/x-png" ||
                imageUpload.PostedFile.ContentType == "image/pjpeg" ||
                imageUpload.PostedFile.ContentType == "image/jpeg" ||
                imageUpload.PostedFile.ContentType == "image/bmp" ||
                imageUpload.PostedFile.ContentType == "image/png" ||
                imageUpload.PostedFile.ContentType == "image/gif"))
            {
                ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('Please upload image less than 300k ');</script>");
            }

            else
            {
                ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('Please upload appropriate file ');</script>");

            }
        }
        private static async Task ProcessAsync()
        {
            CloudStorageAccount storageAccount     = null;
            CloudBlobContainer  cloudBlobContainer = null;
            string sourceFile      = null;
            string destinationFile = null;

            // Retrieve the connection string for use with the application. The storage connection string is stored
            // in an environment variable on the machine running the application called storageconnectionstring.
            // If the environment variable is created after the application is launched in a console or with Visual
            // Studio, the shell needs to be closed and reloaded to take the environment variable into account.
            string storageConnectionString = @"DefaultEndpointsProtocol=https;"
                                             + "AccountName=lsmupdates;"
                                             + "AccountKey= 8+/B2HL9guEhlOeJ3x8I086MHz+apW2TigJx3/D8YPpfawNnrqNugrff3y0BTlzvpyQ3LN2G/96irmiEMR/31g ==;"
                                             + "EndpointSuffix=core.windows.net";


            // Check whether the connection string can be parsed.
            if (CloudStorageAccount.TryParse(LsmStorageConnectionString, out storageAccount))
            {
                try
                {
                    // Create the CloudBlobClient that represents the Blob storage endpoint for the storage account.
                    CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();

                    cloudBlobContainer = cloudBlobClient.GetContainerReference("coherentmeterconnection");

                    // List the blobs in the container.
                    Console.WriteLine("Listing blobs in container.");
                    BlobContinuationToken blobContinuationToken = null;
                    do
                    {
                        var results = await cloudBlobContainer.ListBlobsSegmentedAsync(null, blobContinuationToken);

                        // Get the value of the continuation token returned by the listing call.
                        blobContinuationToken = results.ContinuationToken;
                        foreach (IListBlobItem item in results.Results)
                        {
                            Console.WriteLine(item.Uri);
                        }
                    } while (blobContinuationToken != null); // Loop while the continuation token is not null.
                    Console.WriteLine();
#if false
                    UploadBlob(cloudBlobContainer, sourceFile, localFileName);
                    DownloadBlob(cloudBlockBlob, sourceFile, destinationFile);
#endif
                }
                catch (StorageException ex)
                {
                    Console.WriteLine("Error returned from the service: {0}", ex.Message);
                }
                finally
                {
                    Console.WriteLine("Press any key to delete the sample files and example container.");
                    Console.ReadLine();
                    // Clean up resources. This includes the container and the two temp files.
                    Console.WriteLine("Deleting the container and any blobs it contains");
                    if (cloudBlobContainer != null)
                    {
                        await cloudBlobContainer.DeleteIfExistsAsync();
                    }
                    Console.WriteLine("Deleting the local source file and local downloaded files");
                    Console.WriteLine();
                    File.Delete(sourceFile);
                    File.Delete(destinationFile);
                }
            }
            else
            {
                Console.WriteLine(
                    "A connection string has not been defined in the system environment variables. " +
                    "Add a environment variable named 'storageconnectionstring' with your storage " +
                    "connection string as a value.");
            }
        }
        public static void AcquireAndReleaseLeaseTest(BlobContext context, string containerName, string blobName)
        {
            CloudStorageAccount account = new CloudStorageAccount(new StorageCredentials(context.Account, context.Key), false);
            CloudBlobClient client = new CloudBlobClient(new Uri(context.Address), account.Credentials);
            CloudBlobContainer container = client.GetContainerReference(containerName);
            CloudBlockBlob blob = container.GetBlockBlobReference(blobName);

            BlobTestBase.UploadText(blob, "Text sent to cloud", Encoding.UTF8);

            // acquire a release on the blob and check LeaseStatus to be "locked"
            OperationContext opContext = new OperationContext();
            HttpWebRequest blobRequest = BlobHttpWebRequestFactory.Lease(
                blob.Uri,
                context.Timeout,
                LeaseAction.Acquire,
                null /* proposed lease ID */,
                60 /* lease duration */,
                null /* break period */,
                null /* access condition */,
                opContext);
            BlobTests.SignRequest(blobRequest, context);
            string leaseId = null;
            using (HttpWebResponse response = (HttpWebResponse)blobRequest.GetResponse())
            {
                leaseId = response.Headers["x-ms-lease-id"];
                Assert.AreEqual<HttpStatusCode>(response.StatusCode, HttpStatusCode.Created);
            }

            blob.FetchAttributes();
            Assert.AreEqual<LeaseStatus>(blob.Properties.LeaseStatus, LeaseStatus.Locked);

            // release the release on the blob and check LeaseStatus to be "unlocked"
            opContext = new OperationContext();
            blobRequest = BlobHttpWebRequestFactory.Lease(
                blob.Uri,
                context.Timeout,
                LeaseAction.Release,
                null /* proposed lease ID */,
                null /* lease duration */,
                null /* break period */,
                AccessCondition.GenerateLeaseCondition(leaseId),
                opContext);
            BlobTests.SignRequest(blobRequest, context);
            using (HttpWebResponse response = (HttpWebResponse)blobRequest.GetResponse())
            {
                Assert.AreEqual<HttpStatusCode>(response.StatusCode, HttpStatusCode.OK);
            }

            blob.FetchAttributes();
            Assert.AreEqual<LeaseStatus>(blob.Properties.LeaseStatus, LeaseStatus.Unlocked);

            blob.Delete();
        }
Пример #30
0
        /// <summary>
        /// Removes file from storage account
        /// </summary>
        /// <param name="storageName">Store which has file to remove</param>
        /// <param name="storageKey">Store access key</param>
        private static void RemoveFile(string storageName, string storageKey)
        {
            string baseAddress = General.BlobEndpointUri(storageName);
            var credentials = new StorageCredentialsAccountAndKey(storageName, storageKey);
            var client = new CloudBlobClient(baseAddress, credentials);

            CloudBlobContainer container = client.GetContainerReference(ContainerName);
            if (Exists(container))
            {
                container.Delete();
            }
        }
Пример #31
0
        private async static Task ProcessFiles()
        {
            //0. Constants
            const string AccountName = "jmmmediaservices";
            const string AccountKey  = "KOs3A87L2UydtogQDjM1/FF0yuTaqsqBViFJG+WoKj8=";

            //1. Install Nuget packages
            //1.1 Nuget: Install-Package windowsazure.mediaservices

            ////2. Get AMS context
            //var context = new CloudMediaContext(AccountName, AccountKey);


            //foreach (var asset in context.Assets.ToList())
            //{
            //    Console.WriteLine(asset.Name);
            //}

            //Console.ReadLine();

            //1. From TTML to JSON
            var             storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=jmmmediaservicesstorage;AccountKey=VpmaM9avmqz7zYr/CCtMUndgZserSIE26sYRS/IqInzlPC03KW9BR+DHIOGV/AOPYxZMnTMSOtf1jOojs8OS/Q==");
            CloudBlobClient blobClient     = storageAccount.CreateCloudBlobClient();

            //clean collection
            DocumentDBRepository <AudioFile> .DeleteCollection();

            var containers = blobClient.ListContainers();

            foreach (var container in containers)
            {
                var blobs = blobClient.GetContainerReference(container.Name).ListBlobs().OfType <CloudBlob>().Where(b => b.Name.EndsWith(".ttml")).OrderByDescending(b => b.Properties.Length);

                foreach (var blob in blobs)
                {
                    var ttml = blobClient.GetBlobReferenceFromServer(blob.Uri);

                    using (var stream = new MemoryStream())
                    {
                        ttml.DownloadToStream(stream);
                        stream.Position = 0;
                        var json = Helper.ParseAudioTranscript(stream);

                        var audioFile = new AudioFile
                        {
                            Title            = blob.Uri.Segments.Last().Replace(".ttml", ""),
                            Url              = blob.Uri.ToString().Replace(".ttml", ""),
                            AudioTranscripts = json
                        };

                        var stringJson = Helper.JsonSerializer(json);

                        Console.WriteLine(stringJson);

                        Console.WriteLine();
                        Console.WriteLine("AudioFile object");
                        Console.WriteLine(Helper.JsonSerializer(audioFile));

                        //2. From JSON to DocumentDB
                        await DocumentDBRepository <AudioFile> .CreateItemAsync(audioFile);

                        var jsonContainer = blobClient.GetContainerReference(ttml.Container.Name);

                        var jsonFile = jsonContainer.GetBlockBlobReference(ttml.Name.Replace(".ttml", ".json"));

                        jsonFile.UploadText(stringJson);
                    }
                }
            }
            Console.WriteLine();
            Console.WriteLine("Done!");

            Console.ReadLine();
        }
Пример #32
0
        public static string UploadAndProcessAvatarImage(HttpPostedFileBase file, int maxWidth = MAX_HEIGHT_OR_WIDTH)
        {
            if (file == null)
            {
                return(null);
            }
            string fileName = replaceInvalidCharacters(Path.GetFileName(file.FileName));
            string head;
            string tail;

            splitFileName(fileName, out head, out tail);
            if (tail == "")
            {
                tail = ".jpg";
            }

            // Check is valid file type
            bool valid = isValidTail(tail);

            if (!valid)
            {
                return("-1");
            }

            // Check is valid size
            double size = (double)file.ContentLength / 1024.0; // kB

            if (size > MAX_IMAGE_SIZE)
            {
                return("-2");
            }

            string newFileName = head + "-" + rand.Next(1000, 9999).ToString();

            CloudBlobClient blobClient = new CloudBlobClient(new Uri(STORAGE_URI), new StorageCredentials(STORAGE_ACCOUNT_NAME, STORAGE_ACCOUNT_PRIMARY_ACCESS_KEY));

            CloudBlobContainer container = blobClient.GetContainerReference(STORAGE_CONTAINER_NAME);

            using (Stream inputStream = file.InputStream) {
                var imageStream = new MemoryStream();
                file.InputStream.CopyTo(imageStream); // For some reason needs to be copied
                var imageStreamPure = new MemoryStream();

                Image img = System.Drawing.Image.FromStream(imageStream);

                // Prevent rotation in iOs devices
                if (tail.ToUpper() == ".JPG" || tail.ToUpper() == ".JPEG")
                {
                    var orientation = 0;
                    try { orientation = (int)img.GetPropertyItem(274).Value[0]; } catch {} // 0 if not found
                    switch (orientation)
                    {
                    case 1:
                        // No rotation required.
                        break;

                    case 2:
                        img.RotateFlip(RotateFlipType.RotateNoneFlipX);
                        break;

                    case 3:
                        img.RotateFlip(RotateFlipType.Rotate180FlipNone);
                        break;

                    case 4:
                        img.RotateFlip(RotateFlipType.Rotate180FlipX);
                        break;

                    case 5:
                        img.RotateFlip(RotateFlipType.Rotate90FlipX);
                        break;

                    case 6:
                        img.RotateFlip(RotateFlipType.Rotate90FlipNone);
                        break;

                    case 7:
                        img.RotateFlip(RotateFlipType.Rotate270FlipX);
                        break;

                    case 8:
                        img.RotateFlip(RotateFlipType.Rotate270FlipNone);
                        break;
                    }

                    img.Save(imageStreamPure, ImageFormat.Jpeg);
                }
                else
                {
                    imageStreamPure = imageStream;
                }

                CloudBlockBlob blockBlob3   = container.GetBlockBlobReference(newFileName + tail);
                var            imageStream3 = getResizedPictureStream(imageStreamPure, MAX_HEIGHT_OR_WIDTH, tail);
                saveBlob(file, blockBlob3, imageStream3);
            }
            return(newFileName + tail);
        }
Пример #33
0
        protected void Page_Load(object sender, EventArgs e)
        {
            String recNums             = "default";
            String containerNameConStr = "ContainerConnectionString";
            String storageConStr       = "StorageConnectionString";

            if (Request.QueryString["recordNumber"] != null && Request.QueryString["docType"] != null)
            {
                if (authorizedownload())
                {
                    try
                    {
                        recNums = (Request.QueryString["recordNumber"]).Trim();
                        List <String> downLoadFilesCollection = new List <String>();
                        downLoadFilesCollection = recNums.Split(',').ToList();
                        containerNameConStr     = (Request.QueryString["docType"]).Trim() + containerNameConStr;
                        storageConStr           = (Request.QueryString["docType"]).Trim() + storageConStr;

                        String containerName = ConfigurationManager.ConnectionStrings[containerNameConStr].ConnectionString;

                        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
                            ConfigurationManager.ConnectionStrings[storageConStr].ConnectionString);

                        // Create the blob client.
                        CloudBlobClient    blobClient = storageAccount.CreateCloudBlobClient();
                        CloudBlobContainer container  = blobClient.GetContainerReference(containerName);

                        //ZIp File Name
                        var downloadzipFileName = string.Format((Request.QueryString["docType"]).Trim(), DateTime.Now.ToString());
                        Response.ContentType = "application/zip";
                        Response.AddHeader("Content-Disposition", "attachment; filename=" + downloadzipFileName + ".zip");
                        byte[]          buffer          = new byte[4096];
                        ZipOutputStream zipoutputstream = new ZipOutputStream(Response.OutputStream);
                        zipoutputstream.SetLevel(3);

                        // Create Zip File by looping in the collection

                        foreach (String downloadFile in downLoadFilesCollection)
                        {
                            // Retrieve reference to a blob named blobName
                            CloudBlockBlob         blockBlob = container.GetBlockBlobReference(downloadFile.Trim());
                            System.IO.MemoryStream mem       = new System.IO.MemoryStream();
                            blockBlob.DownloadToStream(mem);
                            int      count    = (int)mem.Length;
                            ZipEntry zipEntry = new ZipEntry(ZipEntry.CleanName(downloadFile + ".tif"));
                            zipEntry.Size = count;
                            zipoutputstream.PutNextEntry(zipEntry);
                            while (count > 0)
                            {
                                zipoutputstream.Write(mem.ToArray(), 0, count);
                                count = mem.Read(mem.ToArray(), 0, count);
                            }
                        }

                        zipoutputstream.Close();
                        Response.Flush();
                    }
                    catch (Exception ex)
                    {
                        if (ex.Message.Contains("404"))
                        {
                            Response.Write("No Document found");
                        }
                    }
                }
            }
            else
            {
                Response.Write("Please provide the parameters");
            }
        }
        public void CopyFromToRestoreSnapshot(BlobContext context, string containerName, string blobName)
        {
            string oldText = "Old stuff";
            string newText = "New stuff";

            StorageCredentials accountAndKey = new StorageCredentials(context.Account, context.Key);
            CloudStorageAccount account = new CloudStorageAccount(accountAndKey, false);
            CloudBlobClient blobClient = new CloudBlobClient(new Uri(context.Address), account.Credentials);
            CloudBlobContainer container = blobClient.GetContainerReference(containerName);
            CloudBlockBlob blob = container.GetBlockBlobReference(blobName);
            BlobTestBase.UploadText(blob, oldText, Encoding.UTF8);
            CloudBlockBlob snapshot = blob.CreateSnapshot();
            Assert.IsNotNull(snapshot.SnapshotTime);
            BlobTestBase.UploadText(blob, newText, Encoding.UTF8);

            Uri sourceUri = new Uri(snapshot.Uri.AbsoluteUri + "?snapshot=" + Request.ConvertDateTimeToSnapshotString(snapshot.SnapshotTime.Value));
            OperationContext opContext = new OperationContext();
            HttpWebRequest request = BlobHttpWebRequestFactory.CopyFrom(blob.Uri, 30, sourceUri, null, null, opContext);
            Assert.IsTrue(request != null, "Failed to create HttpWebRequest");
            BlobTests.SignRequest(request, context);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Assert.AreEqual<HttpStatusCode>(response.StatusCode, HttpStatusCode.Accepted);

            string text = BlobTestBase.DownloadText(blob, Encoding.UTF8);
            Assert.AreEqual<string>(text, oldText);

            blob.Delete(DeleteSnapshotsOption.IncludeSnapshots, null, null);
        }
Пример #35
0
        public IEnumerable <IListBlobItem> ListBlobs(string containerName)
        {
            CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference(containerName);

            return(cloudBlobContainer.ListBlobs());
        }