Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create movetest2.txt #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions movetest2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using Azure;
using Azure.Storage.Files.DataLake;
using Azure.Identity;
using System;
using System.IO;

namespace BlobStorageDemo
{
class Program
{
static void Main(string[] args)
{
string sourceFilePath = "general/landing/test/2023/8/16/file1.txt";
string destinationPath = "secure/folder/test/2023/8/18";
string fileName = "file.txt";

MoveFile(sourceFilePath, destinationPath, fileName);
}

static void MoveFile(string sourceFilePath, string destinationPath, string fileName)
{
string accountName = "your_storage_account_name";
string serviceUrl = $"https://{accountName}.dfs.core.windows.net";

var credential = new DefaultAzureCredential();

string sourceContainer = sourceFilePath.Split('/')[0];
string sourceFileSubPath = Path.Combine(sourceFilePath.Substring(sourceContainer.Length + 1), fileName);
string destinationContainer = destinationPath.Split('/')[0];
string destinationFileSubPath = Path.Combine(destinationPath.Substring(destinationContainer.Length + 1), fileName);

var sourceDataLakeClient = new DataLakeFileSystemClient(new Uri($"{serviceUrl}/{sourceContainer}"), credential);
var destinationDataLakeClient = new DataLakeFileSystemClient(new Uri($"{serviceUrl}/{destinationContainer}"), credential);

var sourceDataLakeFileClient = sourceDataLakeClient.GetFileClient(sourceFileSubPath);

// Ensure the destination path exists
var destinationDirectoryClient = destinationDataLakeClient.GetDirectoryClient(Path.GetDirectoryName(destinationFileSubPath));
destinationDirectoryClient.CreateIfNotExists();

var destinationDataLakeFileClient = destinationDataLakeClient.GetFileClient(destinationFileSubPath);

if (sourceDataLakeFileClient.Exists())
{
// Start the copy operation from source to destination
destinationDataLakeFileClient.StartCopyFromUri(new Uri(sourceDataLakeFileClient.Uri.ToString()));

// Verify that copy has completed, for demonstration purposes.
while (destinationDataLakeFileClient.GetProperties().Value.CopyStatus == Azure.Storage.Blobs.Models.CopyStatus.Pending)
{
System.Threading.Thread.Sleep(1000);
}

if (destinationDataLakeFileClient.GetProperties().Value.CopyStatus == Azure.Storage.Blobs.Models.CopyStatus.Success)
{
// After the successful copy, delete the file from the source container.
sourceDataLakeFileClient.DeleteIfExists();
}
}
else
{
Console.WriteLine("Source file does not exist.");
}
}
}
}