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

时间:2018-05-01 08:53:22

标签: c# azure azure-storage azure-table-storage azure-storage-files

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

val dfFilter4 = df.withColumn("category", when(col(colName) === "CS" && col("id") === 101, 10).otherwise(0))

以下是我尝试阅读文件路径的方法:[插入表格之前]

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;

然而,这个条件

  

if(sampleDir.Exists())

正在失败。而且,控件没有进入循环。

我想将文件共享的路径存储在Azure表存储中。我想获得分区键和行键。怎么做到这一点?任何链接或建议会有帮助吗?感谢。

1 个答案:

答案 0 :(得分:1)

正如@Gaurav所说,在使用上面的代码返回imageFullPath之后,您可以使用以下代码在表存储中存储路径。

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

        }

注意:完整路径是您返回的imageFullPath。

修改: 实体使用从TableEntity派生的自定义类映射到C#对象。 要向表中添加实体,请创建一个定义实体属性的类。

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

上面的代码显示了CloudTable对象的创建,然后是CustomerEntity对象的创建。要准备操作,会创建一个TableOperation对象以将客户实体插入表。最后,通过调用CloudTable.Execute执行操作。

有关详细信息,请参阅此article

更新

  

据我所知,行和分区键是唯一的,因此错误。

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

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