简体   繁体   English

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

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

I am using "Azure Storage File Shares" to store some files from our website, but failed with error message "The specified share already exists".我正在使用“Azure 存储文件共享”存储我们网站上的一些文件,但失败并显示错误消息“指定的共享已存在”。 I have change the file that being upload, but the error persist.我已经更改了正在上传的文件,但错误仍然存​​在。 Here my code这是我的代码

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

Looks like I should not create ShareClient with same name several times.看起来我不应该多次创建具有相同名称的 ShareClient。 Then how to check and use it?那么如何检查和使用呢?

The most important question is, why the file still not yet uploaded (even if I rename the ShareClient object)?最重要的问题是,为什么文件还没有上传(即使我重命名了 ShareClient 对象)?

Looks like I should not create ShareClient with same name several times.看起来我不应该多次创建具有相同名称的 ShareClient。 Then how to check and use it?那么如何检查和使用呢?

You can use ShareClient.CreateIfNotExists instead of ShareClient.Create method.您可以使用ShareClient.CreateIfNotExists代替ShareClient.Create方法。 Former will try to create a share but if a share already exists, then it won't be changed.前者将尝试创建共享,但如果共享已经存在,则不会更改。

You can also use ShareClient.Exists to check if the share exists and then create it using ShareClient.Create if it does not exist.您还可以使用ShareClient.Exists检查共享是否存在,如果不存在则使用ShareClient.Create创建它。 This is not recommended however as it might not work if multiple users are executing that code at the same time.但是,不建议这样做,因为如果多个用户同时执行该代码,它可能不起作用。 Furthermore, you will be making 2 network calls - first to check the existence of share and then the second to create it.此外,您将进行 2 次网络调用 - 第一次检查共享的存在,然后第二次创建共享。

The most important question is, why the file still not yet uploaded (even if I rename the ShareClient object)?最重要的问题是,为什么文件还没有上传(即使我重命名了 ShareClient 对象)?

Your code for uploading the file looks ok to me.您上传文件的代码对我来说看起来不错。 Are you getting any error in that code?您在该代码中是否有任何错误?

We could use ShareClient.CreateIfNotExists when creating ShareClient object to avoid the problem.我们可以在创建 ShareClient 对象时使用 ShareClient.CreateIfNotExists 来避免这个问题。 Like below像下面

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

You might found Similar problem on ShareDirectoryClient.您可能会在 ShareDirectoryClient 上发现类似的问题。 This part purpose is to create the folder structure.这部分的目的是创建文件夹结构。 The upload will fail if the destination folder is not exist.如果目标文件夹不存在,上传将失败。

Error will occur if we create a folder when it already exist.如果我们在已经存在的情况下创建文件夹,则会发生错误。 So, use method ShareDirectoryClient.CreateIfNotExists, like below所以,使用 ShareDirectoryClient.CreateIfNotExists 方法,如下所示

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

Here my complete code这是我的完整代码

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