Search code examples
c#azureazure-storageazure-table-storageazure-storage-files

Read image path from file share and store in table storage Azure


I'm able to upload the image in the Azure file share using below code.

CloudStorageAccount cloudStorageAccount = ConnectionString.GetConnectionString();
            CloudFileClient cloudFileClient = cloudStorageAccount.CreateCloudFileClient();             
            CloudFileShare fileShare = cloudFileClient.GetShareReference("sampleimage");
           if (await fileShare.CreateIfNotExistsAsync())
            {
                await fileShare.SetPermissionsAsync(
                    new FileSharePermissions
                    {
                      
                    });
            }
            //fileShare.CreateIfNotExists();
          
            string imageName = Guid.NewGuid().ToString() + "-" + Path.GetExtension(imageToUpload.FileName);
            CloudFile cloudFile = fileShare.GetRootDirectoryReference().GetFileReference(imageName);
            cloudFile.Properties.ContentType = imageToUpload.ContentType;

            await cloudFile.UploadFromStreamAsync(imageToUpload.InputStream);

            imageFullPath = cloudFile.Uri.ToString();
        }
        catch (Exception ex)
        {

        }
        return imageFullPath;

Here is how I'm trying to read the file path: [Before inserting into a table]

public class ReadFileSharePath
{
    string Path = null;
    public string ReadFilePath()
    {
        
        try
        {
            CloudStorageAccount cloudStorageAccount = ConnectionString.GetConnectionString();
            CloudFileClient cloudFileClient = cloudStorageAccount.CreateCloudFileClient();
            CloudFileShare fileShare = cloudFileClient.GetShareReference("sampleimage");
            if (fileShare.Exists())
            {
                CloudFileDirectory rootdir = fileShare.GetRootDirectoryReference();

                CloudFileDirectory sampleDir = rootdir.GetDirectoryReference("sampleimage");

                if (sampleDir.Exists())
                {
                    // Get a reference to the file we created previously.
                    CloudFile file = sampleDir.GetFileReference("90e94676-492d-4c3c-beb2-1d8d48044e4e-.jpg");

                    // Ensure that the file exists.
                    if (file.Exists())
                    {
                        // Write the contents of the file to the console window.
                        //Console.WriteLine(file.DownloadTextAsync().Result);
                        Path = file.DownloadTextAsync().Result.ToString();
                    }
                }
            }

        }
        catch (Exception)
        {

            throw;
        }
        return Path;
       
    }

}

However, this if condition

if (sampleDir.Exists())

is getting failed. And the control is not entering into loop.

I would like to store the path of file share in the Azure table storage. I would like to get partition key and row key. How can I achieve this?


Solution

  • As @Gaurav said, after you return imageFullPath with above code, you could use following code to store path in table storage.

    void SavePath(string fullpath)
            {
                CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
    
                // Create the table client.
                CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
    
                // Create the CloudTable object that represents the "people" table.
                CloudTable table = tableClient.GetTableReference("people");
    
                // Create a new customer entity.
                CustomerEntity customer1 = new CustomerEntity("joey", "cai");
                customer1.path = fullpath;
    
    
                // Create the TableOperation object that inserts the customer entity.
                TableOperation insertOperation = TableOperation.Insert(customer1);
    
                // Execute the insert operation.
                table.Execute(insertOperation);
            }
            public class CustomerEntity : TableEntity
            {
                public CustomerEntity(string lastName, string firstName)
                {
                    this.PartitionKey = lastName;
                    this.RowKey = firstName;
                }
    
                public CustomerEntity() { }
    
                public string path { get; set; }
    
            }
    

    Note: The fullpath is the imageFullPath that you have returned.

    Edit: Entities map to C# objects by using a custom class derived from TableEntity. To add an entity to a table, create a class that defines the properties of your entity.

    The above code defines an entity class that uses the customer's first name as the row key and last name as the partition key. Together, an entity's partition and row key uniquely identify it in the table. Entities to be stored in tables must be of a supported type, for example derived from the TableEntity class.

    And the above code shows the creation of the CloudTable object and then a CustomerEntity object. To prepare the operation, a TableOperation object is created to insert the customer entity into the table. Finally, the operation is executed by calling CloudTable.Execute.

    For more details, you could refer to this article.

    Update:

    As I understand the Row and Partition key is unique hence error.

    So, when you insert the second entity into the table, it use the same Partition key and Row key. So you could keep the partition key the same and change the row key value. Change the following code to the above:

    CustomerEntity customer1 = new CustomerEntity("joey", "cai"+Guid.NewGuid());
    customer1.path = fullpath;
    TableOperation insertOperation = TableOperation.Insert(customer1);
    table.Execute(insertOperation);