代码之家  ›  专栏  ›  技术社区  ›  Pradeep

无法在指定时间内验证复制源

  •  0
  • Pradeep  · 技术社区  · 7 年前

    我创建了简单的.net核心web api应用程序,在其中添加了一个新的api空控制器,并添加了用于从azure文件共享获取图像/文件并上载到azure blob存储的代码。

    这是我在控制器中编写的代码:

     // GET: api/UploadFiletoBlob
        [HttpGet]
        public async Task<string> UploadFiletoBlob()
        {
            // Retrieve the connection string for use with the application. The storage connection string is stored
            // in an environment variable on the machine running the application called storageconnectionstring.
            // If the environment variable is created after the application is launched in a console or with Visual
            // Studio, the shell needs to be closed and reloaded to take the environment variable into account.
            //string storageConnectionString = Environment.GetEnvironmentVariable("Storageconnectionstring");
    
            // Check whether the connection string can be parsed.
            if (CloudStorageAccount.TryParse(storageConnectionString, out storageAccount))
            {
                try
                {
                    // Create a CloudFileClient object for credentialed access to Azure Files.
                    CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
    
                    // Get a reference to the file share we created previously.
                    CloudFileShare share = fileClient.GetShareReference(fileshareName);
    
                    // Ensure that the share exists.
                    if (await share.ExistsAsync())
                    {
                        // Get a reference to the root directory for the share.
                        CloudFileDirectory rootDir = share.GetRootDirectoryReference();
    
                        // Get a reference to the directory we created previously.
                        CloudFileDirectory sampleDir = rootDir.GetDirectoryReference(directoryName);
    
                        // Ensure that the directory exists.
                        if (await sampleDir.ExistsAsync())
                        {
                            // Get a reference to the file we created previously.
                            sourceFile = sampleDir.GetFileReference(sourceFileName);
    
                            // Ensure that the file exists.
                            if (await sourceFile.ExistsAsync())
                            {
                                // Get a reference to the blob to which the file will be uploaded.
                                CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
                                CloudBlobContainer container = blobClient.GetContainerReference(containerName);
                                // Ensure that the container exists.
                                if (await container.ExistsAsync())
                                {
                                    CloudBlockBlob destBlob = container.GetBlockBlobReference(sourceFile.Name);
                                    // Create a SAS for the file that's valid for 24 hours.
                                    // Note that when you are copying a file to a blob, or a blob to a file, you must use a SAS
                                    // to authorize access to the source object, even if you are copying within the same
                                    // storage account.
                                    string fileSas = sourceFile.GetSharedAccessSignature(new SharedAccessFilePolicy()
                                    {
                                        // Only read permissions are required for the source file.
                                        Permissions = SharedAccessFilePermissions.Read,
                                        SharedAccessExpiryTime = DateTime.UtcNow.AddHours(24)
                                    });
                                    // Construct the URI to the source file, including the SAS token.
                                    Uri fileSasUri = new Uri(sourceFile.StorageUri.PrimaryUri.ToString() + fileSas);
                                    // Upload the file to the blob.
                                    await destBlob.StartCopyAsync(fileSasUri);
                                    // Write the contents of the file to the output window.
                                    Debug.WriteLine("Successfully uploaded the file into blob: {0}", sourceFile.Name);                                 
                                }
                            }
                        }
                    }
                }
                catch (StorageException ex)
                {
                    Console.WriteLine("Error returned from the service: {0}", ex.Message);
                    return "Failed to upload the file into storage blob";
                }
                return $"Successfully uploaded the file into storage blob: {sourceFile.Name}";
            }
            else
            {
                Debug.WriteLine(
                    "A connection string has not been defined in the system environment variables. " +
                    "Add a environment variable named 'storageconnectionstring' with your storage " +
                    "connection string as a value.");
                return "A connection string has not been defined in the system environment variables. " +
                    "Add a environment variable named 'storageconnectionstring' with your storage " +
                    "connection string as a value.";
            }
        }
    

    但是当运行我的应用程序时,我在这行代码中随机得到了下面的异常 await destBlob.StartCopyAsync(fileSasUri); .

    无法在指定的时间内验证复制源

    完成 堆栈跟踪 以上例外情况:

    在Microsoft.WindowsAzure.Storage.Core.Executor.Executor.dúu 4 1.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter 1.获取结果() 在Microsoft.WindowsAzure.Storage.Blob.CloudBlob上。<>c}u DisplayClass111Өu 0。<bӨu 0>d.MoveNext() ---从引发异常的上一个位置的堆栈结束跟踪--- 在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务) 在System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() 下载文件webapi.Controllers.DownloadFilesController.d\uuu 9.MoveNext(),地址:E:\XXX\XXX\DownloadFilesWebAPI\Controllers\DownloadFilesController.cs:line 98

    如何解决这个问题?

    0 回复  |  直到 7 年前
    推荐文章