示例#1
0
        public void TestSlashEquivalence()
        {
            var name = Guid.NewGuid() + "/a";

            var blobClient1 = new BlobClient(AzureCredentials.ConnectionString, AzureCredentials.DefaultBlobContainerName, name);

            Assert.IsFalse(blobClient1.Exists());
            blobClient1.Upload(Stream.Null);
            Assert.IsTrue(blobClient1.Exists());

            var blobClient2 = new BlobClient(AzureCredentials.ConnectionString, AzureCredentials.DefaultBlobContainerName, name.Replace('/', '\\'));

            Assert.IsTrue(blobClient2.Exists());
        }
        // Gets a cursor from container or creates a new cursor if it does not exist
        private static string GetCursor(BlobClient blobClient, BlobChangeFeedClient changeFeedClient, ILogger log)
        {
            string continuationToken = null;

            if (blobClient.Exists())
            {
                // If the continuationToken exists in blob, download and use it
                var stream = new MemoryStream();
                blobClient.DownloadTo(stream);
                continuationToken = Encoding.UTF8.GetString(stream.ToArray());
            }
            else
            {
                // If the continuationToken does not exist in the blob, get the continuationToken from the first item
                Page <BlobChangeFeedEvent> page            = changeFeedClient.GetChanges().AsPages(pageSizeHint: 1).First <Page <BlobChangeFeedEvent> >();
                BlobChangeFeedEvent        changeFeedEvent = page.Values.First <BlobChangeFeedEvent>();
                if (containerCheck.Invoke(changeFeedEvent) && eventTypeCheck.Invoke(changeFeedEvent) && blobCheck.Invoke(changeFeedEvent))
                {
                    log.LogInformation($"event: {changeFeedEvent.EventType} at {changeFeedEvent.Subject.Replace("/blobServices/default", "")} on {changeFeedEvent.EventTime}");
                }
                continuationToken = page.ContinuationToken;
            }

            return(continuationToken);
        }
示例#3
0
        public async Task <Data> ReadAsync(string id)
        {
            BlobClient blob = container.GetBlobClient(id);

            if (blob.Exists())
            {
                using (StreamReader reader = new StreamReader(await blob.OpenReadAsync(), Encoding.UTF8))
                {
                    return(JsonConvert.DeserializeObject <Data>(reader.ReadToEnd()));
                }
            }

            if (BackingDataAccess != null)
            {
                var item = await BackingDataAccess.ReadAsync(id);

                if (item != null)
                {
                    await CreateAsync(item);

                    return(item);
                }
            }

            return(null);
        }
        public static bool BlobExists(string uriString)
        {
            Uri        uri    = new Uri(uriString);
            BlobClient client = new BlobClient(uri);

            return(client.Exists());
        }
示例#5
0
        /// <summary>
        ///
        /// <para>CopyFile:</para>
        ///
        /// <para>Copy a file from a bucket and relative location to another in File Service, caller thread will be blocked before it is done</para>
        ///
        /// <para>EBRemoteFileReadPublicity does nothing as Azure does not support per object authentication, only per container</para>
        ///
        /// <para>Check <seealso cref="IBFileServiceInterface.CopyFile"/> for detailed documentation</para>
        ///
        /// </summary>
        public bool CopyFile(string _SourceBucketName, string _SourceKeyInBucket, string _DestinationBucketName, string _DestinationKeyInBucket, EBRemoteFileReadPublicity _RemoteFileReadAccess = EBRemoteFileReadPublicity.AuthenticatedRead, Action <string> _ErrorMessageAction = null)
        {
            try
            {
                BlobContainerClient SourceClient = AServiceClient.GetBlobContainerClient(_SourceBucketName);
                BlobClient          SourceBlob   = SourceClient.GetBlobClient(_SourceKeyInBucket);

                //Will throw exception on failure
                Response <bool> ExistsResponse = SourceBlob.Exists();

                if (ExistsResponse.Value)
                {
                    BlobContainerClient DestClient = AServiceClient.GetBlobContainerClient(_DestinationBucketName);
                    BlobClient          DestBlob   = SourceClient.GetBlobClient(_DestinationKeyInBucket);

                    CopyFromUriOperation CopyOp = DestBlob.StartCopyFromUri(SourceBlob.Uri, new BlobCopyFromUriOptions());

                    CopyOp.WaitForCompletionAsync();
                    CopyOp.UpdateStatus();

                    return(CopyOp.HasCompleted);
                }
                else
                {
                    _ErrorMessageAction?.Invoke($"BFileServiceAZ -> CopyFile : Source file does not exist");
                    return(false);
                }
            }
            catch (Exception ex)
            {
                _ErrorMessageAction?.Invoke($"BFileServiceAZ -> CopyFile : {ex.Message}\n{ex.StackTrace}");
                return(false);
            }
        }
示例#6
0
        public async Task UploadBlobAsync(IFormFile blobFile, string title, string description)
        {
            // Get a reference of blob
            BlobClient blobClient = _blobContainerClient.GetBlobClient(blobFile.FileName);

            if (!blobClient.Exists())
            {
                // full path to file in temp location
                var filePath = Path.GetTempFileName(); //we are using Temp file name just for the example. Add your own file path.
                if (blobFile.Length > 0)
                {
                    using (var blobStream = new FileStream(filePath, FileMode.Create))
                    {
                        //Copy stream to temp file
                        await blobFile.CopyToAsync(blobStream);

                        //Set a metadata to a blob
                        //await blobClient.SetMetadataAsync(title, description);

                        //Upload blob data to Blob Storage
                        blobStream.Position = 0;
                        await blobClient.UploadAsync(blobStream);
                    }
                }

                //Delete temp file
                if (File.Exists(filePath))
                {
                    File.Delete(filePath);
                }
            }
        }
        /// <summary>Check to see if a blob already exists in the specified container</summary>
        /// <param name="containerName"></param>
        /// <param name="blobName">File or blob name to check for</param>
        /// <returns></returns>
        /// <remarks>Remember to include a file extension</remarks>
        public bool BlobExists(string containerName, string blobName)
        {
            // blob container name - can we set a default somehow
            if (containerName == null || blobName == null)
            {
                throw new System.ArgumentNullException("Arguments can not be null.");
            }

            containerName = containerName.Trim();
            blobName      = blobName.Trim();

            if (containerName == String.Empty || blobName == String.Empty)
            {
                return(false);
            }

            // Get a reference to a share and then create it
            BlobContainerClient container = new BlobContainerClient(this.ConnectionString, containerName);

            // check the container exists
            Response <bool> exists = container.Exists();

            if (!exists.Value)
            {
                return(false);
            }

            // Get a reference to the blob name
            BlobClient blob = container.GetBlobClient(blobName);

            exists = blob.Exists();

            return(exists.Value);
        }
示例#8
0
        /// <summary>
        /// Uploads the image to azure blob storage
        /// </summary>
        /// <param name="file"> file to upload </param>
        /// <returns> new BlobClient object </returns>
        public async Task <BlobClient> UploadImage(IFormFile file)
        {
            BlobContainerClient container = new BlobContainerClient(Configuration["ImageBlob"], "images");

            await container.CreateIfNotExistsAsync();

            BlobClient blob = container.GetBlobClient(file.FileName);

            using var stream = file.OpenReadStream();

            BlobUploadOptions options = new BlobUploadOptions()
            {
                HttpHeaders = new BlobHttpHeaders()
                {
                    ContentType = file.ContentType
                }
            };

            if (!blob.Exists())
            {
                await blob.UploadAsync(stream, options);
            }

            return(blob);
        }
示例#9
0
        /// <summary>
        /// Read the JSON status file to update the history of the given grabbers
        /// </summary>
        private static void UpdateGrabberHistory(Grabber[] grabbers, BlobContainerClient container, ILogger log)
        {
            log.LogInformation($"reading information from stored grabber file: {GRABBERS_JSON_FILENAME}");
            BlobClient blob = container.GetBlobClient(GRABBERS_JSON_FILENAME);

            if (!blob.Exists())
            {
                return;
            }

            using MemoryStream stream = new MemoryStream();
            blob.DownloadTo(stream);
            string json = Encoding.UTF8.GetString(stream.ToArray());

            Grabber[] oldGrabbers = GrabberIO.GrabbersFromJson(json);

            Dictionary <string, Grabber> oldGrabberDictionary = new Dictionary <string, Grabber>();

            foreach (Grabber oldGrabber in oldGrabbers)
            {
                if (oldGrabberDictionary.ContainsKey(oldGrabber.Info.ID))
                {
                    oldGrabberDictionary.Remove(oldGrabber.Info.ID);
                }

                oldGrabberDictionary.Add(oldGrabber.Info.ID, oldGrabber);
            }

            foreach (Grabber grabber in grabbers.Where(x => oldGrabberDictionary.ContainsKey(x.Info.ID)))
            {
                grabber.History.Update(oldGrabberDictionary[grabber.Info.ID].History);
            }

            log.LogInformation($"read information about {grabbers.Length} grabbers");
        }
示例#10
0
        private async Task <BlobClient> CreateBlogInfoIfNotExists(
            BlogInfo blogInfo,
            string contentFileRoot,
            string storageConnectionString)
        {
            if (blogInfo.Name == null)
            {
                throw new ApplicationException(
                          "Pass in a unique blogname in the list of names.");
            }
            // Flesh out the blog content path
            GetBlogContentPath(blogInfo, contentFileRoot);

            // Store all info in a single container
            BlobContainerClient container = new BlobContainerClient(
                storageConnectionString,
                "bloginfo");

            container.CreateIfNotExists();

            // create a new blob with the name and store
            // all metadata for the blob in that name
            BlobClient blob       = container.GetBlobClient(blogInfo.Name);
            bool       blobExists = blob.Exists();

            if (!blobExists)
            {
                await CreateNewBlog(blogInfo, blob);
            }
            return(blob);
        }
        static string ReadBlob()
        {
            string              connString        = Environment.GetEnvironmentVariable("AZURE_STORAGE_CONNECTION_STRING");
            BlobServiceClient   blobServiceClient = new BlobServiceClient(connString);
            BlobContainerClient containerClient   = blobServiceClient.GetBlobContainerClient(containerName);

            BlobClient blobClient = containerClient.GetBlobClient(blobName);

            if (blobClient.Exists())
            {
                logger.LogInformation("ReadUpdateBlobStorage; it exists");
                string text;
                using (var memoryStream = new MemoryStream())
                {
                    blobClient.DownloadTo(memoryStream);
                    text = System.Text.Encoding.UTF8.GetString(memoryStream.ToArray());
                }
                return(text);
            }
            else
            {
                // return an empty JSON array if the blob doesn't exist
                return("[]");
            }
        }
示例#12
0
        /// <summary>
        /// Determines whether the blob exists
        /// </summary>
        /// <param name="blobName">The name of the blob</param>
        /// <returns>True if the blob exists</returns>
        public async Task <bool> Exists(string blobName)
        {
            var uri = await auth.GetSasTokenAsync(blobName);

            //build the blob:
            var blob = new BlobClient(uri);

            return(blob.Exists());
        }
示例#13
0
        private static void DeletePdfBlob(object blobName)
        {
            BlobClient blobClient = pdfBlobContainer.GetBlobClient(blobName.ToString());

            Thread.Sleep(1000 * 90);

            if (blobClient.Exists())
            {
                blobClient.Delete();
            }
        }
示例#14
0
        protected void SendBytes(BlobClient blob, byte[] msgBytes)
        {
            if (!overwrite && blob.Exists())
            {
                return;
            }

            using (MemoryStream ms = new MemoryStream(msgBytes))
            {
                blob.Upload(ms, new BlobUploadOptions());
            }
        }
示例#15
0
        public Uri GetPhoto(string fileName)
        {
            BlobContainerClient blobContainerClient = _blobServiceClient.GetBlobContainerClient(CONTAINER_NAME);
            BlobClient          blobClient          = blobContainerClient.GetBlobClient("main" + fileName);

            if (!blobClient.Exists())
            {
                return(new Uri(DEFAULT_PHOTO_URL));
            }

            return(blobClient.Uri);
        }
示例#16
0
 public void CreateEmptyFile()
 {
     if (!BlobClient.Exists())
     {
         BlobClient.Upload(new MemoryStream(0));
     }
     // if (BlobClient.Exists() && !LocalCachePath.Exists)
     // {
     //     BlobClient.DownloadTo(LocalCachePath.FullName);
     //     LocalCachePath.Refresh();
     // }
 }
示例#17
0
        static void Main(string[] args)
        {
            try
            {
                BlobServiceClient   blobServiceClient = new BlobServiceClient(connString);
                BlobContainerClient containerClient   = blobServiceClient.GetBlobContainerClient(containerName);
                containerClient.CreateIfNotExists(); //Creating Container

                #region Code
                //Check Existence
                //if(!containerClient.Exists())
                //{
                //    containerClient = blobServiceClient.CreateBlobContainer(containerName);
                //}
                #endregion

                File.WriteAllText(localFilePath, "This is a blob");

                BlobClient blobClient = containerClient.GetBlobClient(fileName); //Getting Blob

                //Check for blobclient existence
                if (!blobClient.Exists())
                {
                    using (FileStream uploadFileStream = File.OpenRead(localFilePath))
                    {
                        blobClient.UploadAsync(uploadFileStream);
                    }
                }


                foreach (BlobItem item in containerClient.GetBlobs())
                {
                    BlobClient       blbClient        = containerClient.GetBlobClient(item.Name);
                    BlobDownloadInfo download         = blbClient.Download();
                    string           downloadFilePath = Path.Combine("../downloadeddata/", item.Name);

                    using (FileStream downloadFileStream = File.OpenWrite(downloadFilePath))
                    {
                        download.Content.CopyToAsync(downloadFileStream);
                        downloadFileStream.Close();
                    }
                }



                Console.ReadLine();
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.Message);
            }
        }
示例#18
0
        private static File GetFileInfo(BlobClient blob, BlobSasBuilder readPermissions)
        {
            File file = null;

            if (blob.Exists())
            {
                var props = blob.GetProperties().Value;

                file = GetFileInfo(blob, readPermissions, props);
            }

            return(file);
        }
示例#19
0
        public BlobDownloadInfo ShowImage(string file)
        {
            BlobServiceClient   blobServiceClient = new BlobServiceClient(_connStr.Value.Storage);
            BlobContainerClient containerClient   = blobServiceClient.GetBlobContainerClient("images-staging");
            BlobClient          blobClient        = containerClient.GetBlobClient(file);

            if (!blobClient.Exists())
            {
                throw new FileNotFoundException("Blob not found", file);
            }

            return(blobClient.Download());
        }
示例#20
0
        private async Task <FollowVM> GetItemsAsync(ApplicationUser user)
        {
            string currentUser = _userManager.GetUserId(HttpContext.User);


            // 2. Connect to Azure Storage account.
            var connectionString = _configuration.GetConnectionString("AccessKey");
            BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);

            // 3. Use container for users profile photos.
            string containerName = "profilephotos";
            BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);

            // 4. Create new blob and upload to azure storage account.
            BlobClient blobClient = containerClient.GetBlobClient("profile-" + user.Id + ".png");

            if (!blobClient.Exists())
            {
                blobClient = containerClient.GetBlobClient("profiledefault.png");
            }

            //BlobDownloadInfo download = await blobClient.DownloadAsync();

            byte[] result = null;
            using (var ms = new MemoryStream())
            {
                blobClient.DownloadTo(ms);
                result = ms.ToArray();
            }


            string base64String = Convert.ToBase64String(result);

            string image = String.Format("data:image/png;base64,{0}", base64String);

            FollowVM followVM = new FollowVM();


            var userFollow = await(from uf in _context.UserFollows
                                   where uf.UserParentId.Equals(currentUser) &&
                                   uf.UserChildId.Equals(user.Id)
                                   select uf).FirstOrDefaultAsync();

            followVM.DisplayName      = user.DisplayName;
            followVM.UserName         = user.UserName;
            followVM.ProfileBiography = user.Biography;
            followVM.AzurePhoto       = image;
            followVM.UserFollow       = userFollow;

            return(followVM);
        }
        public async Task <Stream> LoadImage(UserArc userArc, byte imageType, string imageFileUri)
        {
            String []           imageUriArray = imageFileUri.Split('/');
            String              imageFileName = WebUtility.UrlDecode(imageUriArray[imageUriArray.Length - 1]);
            BlobContainerClient blobContainer = await GetOrCreateCloudBlobContainer(GetUploadPicContainerName(), PublicAccessType.None);

            BlobClient imageBlob = blobContainer.GetBlobClient(imageFileName);

            if (!imageBlob.Exists())
            {
                return(null);
            }
            return(await imageBlob.OpenReadAsync());
        }
示例#22
0
        public async Task DeleteAsync(Data item)
        {
            BlobClient blob = container.GetBlobClient(item.Key);

            if (blob.Exists())
            {
                await blob.DeleteAsync();
            }

            if (BackingDataAccess != null)
            {
                await BackingDataAccess.DeleteAsync(item);
            }
        }
示例#23
0
        public void Delete(string blobName)
        {
            Debug.WriteLine("Deleting blob:\n\t {0}\n", blobName);
            BlobClient blobClient = GetBlobClient(blobName);

            if (blobClient.Exists().Value)
            {
                blobClient.Delete();
            }
            else
            {
                throw new InvalidOperationException("Blob not found");
            }
        }
        private async Task <List <ProfileVM> > GetItemsAsync(string userName)
        {
            var usersDropDown = await(from user in _context.ApplicationUsers
                                      where user.UserName.Contains(userName)
                                      select user).ToListAsync();

            List <ProfileVM> profileVMs = new List <ProfileVM>();

            foreach (var usersDropDow in usersDropDown)
            {
                ProfileVM profileVM = new ProfileVM();

                // 2. Connect to Azure Storage account.
                var connectionString = _configuration.GetConnectionString("AccessKey");
                BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);

                // 3. Use container for users profile photos.
                string containerName = "profilephotos";
                BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);

                // 4. Create new blob and upload to azure storage account.
                BlobClient blobClient = containerClient.GetBlobClient("profile-" + usersDropDow.Id + ".png");

                if (!blobClient.Exists())
                {
                    blobClient = containerClient.GetBlobClient("profiledefault.png");
                }

                //BlobDownloadInfo download = await blobClient.DownloadAsync();

                byte[] result = null;
                using (var ms = new MemoryStream())
                {
                    blobClient.DownloadTo(ms);
                    result = ms.ToArray();
                }


                string base64String = Convert.ToBase64String(result);

                string image = String.Format("data:image/png;base64,{0}", base64String);

                profileVM.AzurePhoto  = image;
                profileVM.UserName    = usersDropDow.UserName;
                profileVM.DisplayName = usersDropDow.DisplayName;
                profileVMs.Add(profileVM);
            }

            return(profileVMs);
        }
示例#25
0
        public async Task <string> UploadFile(Stream stream, string fileName)
        {
            await _container.CreateIfNotExistsAsync();

            await _container.SetAccessPolicyAsync(PublicAccessType.Blob);

            BlobClient blob = _container.GetBlobClient(fileName);

            if (!blob.Exists())
            {
                await blob.UploadAsync(stream);
            }
            return(blob.Uri.ToString());
        }
示例#26
0
        public static ConfigurationSettings LoadFromBlobStorage()
        {
            ConfigurationSettings settings = null;

            string containerName    = "configuration";
            string connectionString = ConfigurationManager.AppSettings["AZURE_STORAGE_CONNECTION_STRING"];
            int    retryCount       = 0;
            int    maxRetryCount    = 3;
            int    retryDelay       = 300;

            // Create a BlobServiceClient object which will be used to create a container client
            BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);

            // Create the container and return a container client object
            BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);

            // Get a reference to a blob
            BlobClient blobClient = containerClient.GetBlobClient("configurationSettings.json");

            do
            {
                if (blobClient.Exists())
                {
                    using (StreamReader reader = new StreamReader(blobClient.OpenRead()))
                    {
                        try
                        {
                            string json = reader.ReadToEnd();
                            settings = JsonConvert.DeserializeObject <ConfigurationSettings>(json);
                            break;
                        }
                        catch // Read failed
                        {
                            retryCount++;
                            retryDelay = retryDelay * retryCount;
                            Thread.Sleep(retryDelay);
                        }
                    }
                }
                else
                {
                    retryCount++;
                    retryDelay = retryDelay * retryCount;
                    Thread.Sleep(retryDelay);
                }
            } while (retryCount <= maxRetryCount);

            return(settings);
        }
示例#27
0
        public async Task <JsonResult> OnPostUploadProfilePhoto(IFormFile ProfilePhoto)
        {
            var returnMessage = "failure";

            string userId = _userManager.GetUserId(User);


            // 1. Convert image to byte array for photo manipulation.
            byte[] result = null;
            using var fileStream = ProfilePhoto.OpenReadStream();
            using (var outStream = new MemoryStream())
            {
                var imageStream = Image.FromStream(fileStream);

                var height    = (150 * imageStream.Height) / imageStream.Width;
                var thumbnail = imageStream.GetThumbnailImage(150, 150, null, IntPtr.Zero);

                using (var thumbnailStream = new MemoryStream())
                {
                    thumbnail.Save(thumbnailStream, ImageFormat.Png);
                    result = thumbnailStream.ToArray();
                }
            }

            // 2. Connect to Azure Storage account.
            var connectionString = _configuration.GetConnectionString("AccessKey");
            BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);

            // 3. Use container for users profile photos.
            string containerName = "profilephotos";
            BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);

            // 4. Create new blob and upload to azure storage account.
            var        stream     = new MemoryStream(result);
            BlobClient blobClient = containerClient.GetBlobClient("profile-" + userId + ".png");

            if (!blobClient.Exists())
            {
                await blobClient.UploadAsync(stream, true);
            }
            else
            {
                await blobClient.DeleteAsync();

                await blobClient.UploadAsync(stream, true);
            }

            return(new JsonResult(returnMessage));
        }
        private static List <DownloadRecord> LoadRecords(BlobContainerClient container, string packageName)
        {
            string     fileName = $"logs/{packageName.ToLower()}.json";
            BlobClient blob     = container.GetBlobClient(fileName);

            if (!blob.Exists())
            {
                throw new InvalidOperationException($"file not found: {fileName}");
            }
            using MemoryStream stream = new();
            blob.DownloadTo(stream);
            string json = Encoding.UTF8.GetString(stream.ToArray());

            return(IO.FromJson(json));
        }
示例#29
0
        private async Task <bool> UploadImageToAzureStorage(string imageUri)
        {
            var fileName           = Path.GetFileName(imageUri);
            var blobUri            = new Uri($"{Environment.GetEnvironmentVariable("BlobContainerUrl")}/{fileName}");
            var storageCredentials = new StorageSharedKeyCredential(
                Environment.GetEnvironmentVariable("StorageAccount"),
                Environment.GetEnvironmentVariable("StorageKey"));
            var blobClient = new BlobClient(blobUri, storageCredentials);

            if (!blobClient.Exists())
            {
                await blobClient.StartCopyFromUriAsync(new Uri(imageUri));
            }

            return(await Task.FromResult(true));
        }
示例#30
0
        public IActionResult Create([FromForm] FileCreate apiModel)
        {
            BlobContainerClient containerClient = _blobSC.GetBlobContainerClient($"pilot{apiModel.PilotID}");

            if (!containerClient.Exists())
            {
                containerClient = _blobSC.CreateBlobContainer($"pilot{apiModel.PilotID}", Azure.Storage.Blobs.Models.PublicAccessType.BlobContainer);
            }
            BlobClient blobClient = containerClient.GetBlobClient(apiModel.FileName);

            if (blobClient.Exists())
            {
                return(BadRequest(new { error = "File name already taken for this pilot" }));
            }
            blobClient.Upload(apiModel.File.OpenReadStream());
            apiModel.FileURL = blobClient.Uri.AbsoluteUri;
            return(Ok(FileMinimal.FromDLModel(_aviBL.AddFile(apiModel.ToDLModel()))));
        }