繁体   English   中英

Azure 存储文件共享上已存在指定的共享

[英]The specified share already exists on Azure Storage File Shares

我正在使用“Azure 存储文件共享”存储我们网站上的一些文件,但失败并显示错误消息“指定的共享已存在”。 我已经更改了正在上传的文件,但错误仍然存​​在。 这是我的代码

        public static void Test2Upload()
    {
        System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
        string connectionString = "DefaultEndpointsProtocol=https;AccountName=xxxxx;AccountKey=xxxxx;EndpointSuffix=core.windows.net";
string shareName = "myapp-dev";
        string dirName = "files";
        string fileName = "catto.jpg";

        // Path to the local file to upload
        string localFilePath = @"d:\temp\two.jpg";

        // Get a reference to a share and then create it
        ShareClient share = new ShareClient(connectionString, shareName);
        share.Create();

        // Get a reference to a directory and create it
        ShareDirectoryClient directory = share.GetDirectoryClient(dirName);
        directory.Create();

        // Get a reference to a file and upload it
        ShareFileClient file = directory.GetFileClient(fileName);
        using (FileStream stream = File.OpenRead(localFilePath))
        {
            file.Create(stream.Length);
            file.UploadRange(
                new HttpRange(0, stream.Length),
                stream);
        }
    }   

看起来我不应该多次创建具有相同名称的 ShareClient。 那么如何检查和使用呢?

最重要的问题是,为什么文件还没有上传(即使我重命名了 ShareClient 对象)?

看起来我不应该多次创建具有相同名称的 ShareClient。 那么如何检查和使用呢?

您可以使用ShareClient.CreateIfNotExists代替ShareClient.Create方法。 前者将尝试创建共享,但如果共享已经存在,则不会更改。

您还可以使用ShareClient.Exists检查共享是否存在,如果不存在则使用ShareClient.Create创建它。 但是,不建议这样做,因为如果多个用户同时执行该代码,它可能不起作用。 此外,您将进行 2 次网络调用 - 第一次检查共享的存在,然后第二次创建共享。

最重要的问题是,为什么文件还没有上传(即使我重命名了 ShareClient 对象)?

您上传文件的代码对我来说看起来不错。 您在该代码中是否有任何错误?

我们可以在创建 ShareClient 对象时使用 ShareClient.CreateIfNotExists 来避免这个问题。 像下面

ShareClient share = new ShareClient(connectionString, shareName); 
share.CreateIfNotExists();
 

您可能会在 ShareDirectoryClient 上发现类似的问题。 这部分的目的是创建文件夹结构。 如果目标文件夹不存在,上传将失败。

如果我们在已经存在的情况下创建文件夹,则会发生错误。 所以,使用 ShareDirectoryClient.CreateIfNotExists 方法,如下所示

ShareDirectoryClient directory = share.GetDirectoryClient(dirName);
directory.CreateIfNotExists();
           

这是我的完整代码

public static void TestUpload()
        {
            System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
            string connectionString = "DefaultEndpointsProtocol=https;AccountName=xxx;AccountKey=xx;EndpointSuffix=core.windows.net";

            string shareName = "myapp-dev";
            string dirName = "myfiles";
            string fileName = "catto.jpg";

            string localFilePath = @"d:\temp\two.jpg"; 

            // Get a reference to a share and then create it
            
            ShareClient share = new ShareClient(connectionString, shareName);
            share.CreateIfNotExists();
             
            // Get a reference to a directory and create it
            ShareDirectoryClient directory = share.GetDirectoryClient(dirName);
            directory.CreateIfNotExists();
            

            // Get a reference to a file and upload it
            ShareFileClient file = directory.GetFileClient(fileName);
            using (FileStream stream = File.OpenRead(localFilePath))
            {
                file.Create(stream.Length);
                file.UploadRange(
                    new HttpRange(0, stream.Length),
                    stream);
            }
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM