从文件共享中读取图像路径并存储在 table storage azure

read image path from file share and store in table storage azure

我可以使用以下代码将图像上传到 Azure 文件共享中。

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;

以下是我尝试读取文件路径的方式:[在插入 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;

    }

}

但是,这个if条件

if (sampleDir.Exists())

正在获取 failed.And,控件未进入循环。

我想在 Azure table 存储中存储文件共享的路径。我想获取分区键和行键。如何做到这一点?任何 link 或建议会有所帮助吗?谢谢

正如@Gaurav 所说,在使用上述代码 return imageFullPath 之后,您可以使用以下代码将路径存储在 table 存储中。

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; }

        }

注意:完整路径是您 return编辑的 imageFullPath。

编辑: 实体通过使用从 TableEntity 派生的自定义 class 映射到 C# 对象。 要将实体添加到 table,请创建一个定义实体属性的 class。

以上代码定义了一个实体 class,它使用客户的名字作为行键,姓氏作为分区键。实体的分区和行键一起在 table 中唯一标识它。要存储在 tables 中的实体必须是受支持的类型,例如从 TableEntity class 派生的类型。

并且上面的代码显示了 CloudTable 对象的创建,然后是 CustomerEntity 对象。为了准备操作, 创建了一个 TableOperation 对象以将客户实体插入 table。最后调用CloudTable.Execute.

执行操作

更多细节,你可以参考这个article

更新:

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

因此,当您将第二个实体插入 table 时,它使用相同的分区键和行键。因此,您可以保持分区键不变并更改行键值。 将下面的代码改成上面的:

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