public static async Task TransferFileAsync(AzureFileTransferInfo transferInfo, ISystemConfiguratorProxy systemConfiguratorProxy)
        {
            //
            // C++ Azure Blob SDK not supported for ARM, so use Service to copy file to/from
            // App's LocalData and then use C# Azure Blob SDK to transfer
            //
            var appLocalDataFile = await ApplicationData.Current.TemporaryFolder.CreateFileAsync(transferInfo.BlobName, CreationCollisionOption.ReplaceExisting);

            transferInfo.AppLocalDataPath = appLocalDataFile.Path;

            if (!transferInfo.Upload)
            {
                transferInfo.AppLocalDataPath = await DownloadFile(transferInfo, appLocalDataFile);
            }

            // use C++ service to copy file to/from App LocalData
            var request = new AzureFileTransferRequest(transferInfo);
            var result  = await systemConfiguratorProxy.SendCommandAsync(request);

            if (transferInfo.Upload)
            {
                await UploadFile(transferInfo, appLocalDataFile);
            }

            await appLocalDataFile.DeleteAsync();
        }
        public static async Task UploadFile(AzureFileTransferInfo transferInfo, StorageFile appLocalDataFile)
        {
            var blockBlob = await GetBlob(transferInfo, true);

            // Save blob contents to a file.
            await blockBlob.UploadFromFileAsync(appLocalDataFile);
        }
        public static async Task <string> DownloadFile(AzureFileTransferInfo transferInfo, StorageFile appLocalDataFile)
        {
            var appLocalDataPath = appLocalDataFile.Path;

            var blockBlob = await GetBlob(transferInfo, false);

            // Save blob contents to a file.
            await blockBlob.DownloadToFileAsync(appLocalDataFile);

            return(appLocalDataPath);
        }
Пример #4
0
        private async Task <string> UploadDMFile(string jsonParamString)
        {
            Logger.Log("Uploading DM file...", LoggingLevel.Information);

            try
            {
                object paramsObject = JsonConvert.DeserializeObject(jsonParamString);
                if (paramsObject == null || !(paramsObject is JObject))
                {
                    throw new Error(ErrorCodes.INVALID_PARAMS, "Invalid enumDMFiles parameters.");
                }

                JObject jsonParamsObject = (JObject)paramsObject;

                string folderName = GetParameter(jsonParamsObject, JsonFolder, ErrorCodes.INVALID_FOLDER_PARAM, "Invalid or missing folder parameter.");
                string fileName   = GetParameter(jsonParamsObject, JsonFile, ErrorCodes.INVALID_FILE_PARAM, "Invalid or missing folder parameter.");

                var info = new AzureFileTransferInfo();
                info.ConnectionString  = GetParameter(jsonParamsObject, JsonConnectionString, ErrorCodes.INVALID_CONNECTION_STRING_PARAM, "Invalid or missing connection string parameter.");
                info.ContainerName     = GetParameter(jsonParamsObject, JsonContainer, ErrorCodes.INVALID_CONTAINER_PARAM, "Invalid or missing container parameter.");
                info.BlobName          = fileName;
                info.Upload            = true;
                info.RelativeLocalPath = folderName + "\\" + fileName;
                info.AppLocalDataPath  = ApplicationData.Current.TemporaryFolder.Path + "\\" + fileName;

                AzureFileTransferRequest request = new AzureFileTransferRequest(info);
                var response = _systemConfiguratorProxy.SendCommand(request);
                if (response.Status != ResponseStatus.Success)
                {
                    throw new Error(ErrorCodes.ERROR_MOVING_FILE, "SystemConfigurator failed to move file.");
                }

                Logger.Log("File copied to UWP application temporary folder...", LoggingLevel.Information);
                var appLocalDataFile = await ApplicationData.Current.TemporaryFolder.GetFileAsync(fileName);

                Logger.Log("Uploading file...", LoggingLevel.Information);
                await IoTDMClient.AzureBlobFileTransfer.UploadFile(info, appLocalDataFile);

                Logger.Log("Upload done. Deleting local temporary file...", LoggingLevel.Information);
                await appLocalDataFile.DeleteAsync();

                Logger.Log("Temporary file deleted..", LoggingLevel.Information);

                return(BuildMethodJsonResponseString("" /*payload*/, SuccessCode, "" /*error message*/));
            }
            catch (Exception err)
            {
                return(BuildMethodJsonResponseString("", err.HResult, err.Message));
            }
        }
        public async Task <string> DownloadToTempAsync(ISystemConfiguratorProxy systemConfiguratorProxy)
        {
            var info = new AzureFileTransferInfo()
            {
                ConnectionString = ConnectionString,
                ContainerName    = ContainerName,
                BlobName         = BlobName,
                Upload           = false,

                RelativeLocalPath = BlobName
            };

            await AzureBlobFileTransfer.TransferFileAsync(info, systemConfiguratorProxy);

            return(BlobName);
        }
        public async Task <string> DownloadToTempAsync(DeviceManagementClient client)
        {
            var path = DMGarbageCollector.TempFolder + BlobName;
            var info = new AzureFileTransferInfo()
            {
                ConnectionString = ConnectionString,
                ContainerName    = ContainerName,
                BlobName         = BlobName,
                Upload           = false,

                LocalPath = path
            };

            await AzureBlobFileTransfer.TransferFileAsync(info, client);

            return(path);
        }
        private static async Task <CloudBlockBlob> GetBlob(AzureFileTransferInfo transferInfo, bool ensureContainerExists)
        {
            // Retrieve storage account from connection string.
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(transferInfo.ConnectionString);

            // Create the blob client.
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            // Retrieve a reference to a container.
            CloudBlobContainer container = blobClient.GetContainerReference(transferInfo.ContainerName);

            if (ensureContainerExists)
            {
                // Create the container if it doesn't already exist.
                await container.CreateIfNotExistsAsync();
            }

            // Retrieve reference to a named blob.
            return(container.GetBlockBlobReference(transferInfo.BlobName));
        }
        public async Task UpdateConfigWithProfileXmlAsync(string connectionString, IEnumerable <Message.WifiProfileConfiguration> profilesToAdd)
        {
            // Download missing profiles
            foreach (var profile in profilesToAdd)
            {
                var profileBlob            = IoTDMClient.BlobInfo.BlobInfoFromSource(connectionString, profile.Path);
                AzureFileTransferInfo info = new AzureFileTransferInfo()
                {
                    BlobName = profileBlob.BlobName, ConnectionString = profileBlob.ConnectionString, ContainerName = profileBlob.ContainerName
                };
                var storageFile = await ApplicationData.Current.TemporaryFolder.CreateFileAsync(profileBlob.BlobName, CreationCollisionOption.ReplaceExisting);

                var localProfilePath = await AzureBlobFileTransfer.DownloadFile(info, storageFile);

                // strip off temp folder prefix for use with TemporaryFolder.CreateFileAsync
                var contents = await Windows.Storage.FileIO.ReadTextAsync(storageFile);

                var encodedXml = new System.Xml.Linq.XElement("Data", contents);
                profile.Xml = encodedXml.FirstNode.ToString();
                //profile.Xml = SecurityElement.Escape(contents);
                await storageFile.DeleteAsync();
            }
        }
        public static async Task TransferFileAsync(AzureFileTransferInfo transferInfo, DeviceManagementClient client)
        {
            //
            // C++ Azure Blob SDK not supported for ARM, so use Service to copy file to/from
            // App's LocalData and then use C# Azure Blob SDK to transfer
            //
            var appLocalDataFile = await ApplicationData.Current.TemporaryFolder.CreateFileAsync(transferInfo.BlobName, CreationCollisionOption.ReplaceExisting);

            transferInfo.AppLocalDataPath = appLocalDataFile.Path;

            if (!transferInfo.Upload)
            {
                transferInfo.AppLocalDataPath = await DownloadFile(transferInfo, appLocalDataFile);
            }

            await client.TransferFileAsync(transferInfo);

            if (transferInfo.Upload)
            {
                await UploadFile(transferInfo, appLocalDataFile);
            }

            await appLocalDataFile.DeleteAsync();
        }