Skip to content

Commit

Permalink
Can skip existing files when destination is Azure
Browse files Browse the repository at this point in the history
  • Loading branch information
kpfaulkner committed Mar 3, 2017
1 parent 89e0d0f commit bec00f7
Show file tree
Hide file tree
Showing 11 changed files with 160 additions and 26 deletions.
12 changes: 12 additions & 0 deletions azurecopy/Handlers/AzureFileHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ public string GetBlobNameFromUrl(string url)
throw new NotImplementedException();
}

/// <summary>
/// Does blob exists in container
/// </summary>
/// <param name="containerName"></param>
/// <param name="blobName"></param>
/// <returns></returns>
public bool DoesBlobExists(string containerName, string blobName)
{
var exists = false;

return exists;
}

/// <summary>
/// Read blob.
Expand Down
24 changes: 24 additions & 0 deletions azurecopy/Handlers/AzureHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,30 @@ public void CreateCustomAzureClient(string accountName, string accountKey)
client = AzureHelper.GetCloudBlobClient(accountName, accountKey);
}

/// <summary>
/// Does blob exists in container
/// </summary>
/// <param name="containerName"></param>
/// <param name="blobName"></param>
/// <returns></returns>
public bool DoesBlobExists(string containerName, string blobName)
{
var exists = false;
var container = client.GetContainerReference(containerName);
try
{
var blobRef = container.GetBlobReferenceFromServer(blobName);
exists = blobRef.Exists();
}
catch( Exception ex)
{
// so exists method throws an exception when blob doesn't exist. Even though its supposed to return a bool indicating?
// DUH!
}

return exists;
}

/// <summary>
/// Make new Azure container.
/// Assumption being last part of url is the new container.
Expand Down
13 changes: 13 additions & 0 deletions azurecopy/Handlers/DropboxHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,19 @@ private string GetBlobPrefixFromUrl(string url)
return string.Join("/", sp.Skip(3));
}

/// <summary>
/// Does blob exists in container
/// </summary>
/// <param name="containerName"></param>
/// <param name="blobName"></param>
/// <returns></returns>
public bool DoesBlobExists(string containerName, string blobName)
{
var exists = false;

return exists;
}

/// <summary>
/// Gets container name from the full url.
/// This is cloud specific.
Expand Down
13 changes: 13 additions & 0 deletions azurecopy/Handlers/FileSystemHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ public FileSystemHandler(string url)
baseUrl = url;
}

/// <summary>
/// Does blob exists in container
/// </summary>
/// <param name="containerName"></param>
/// <param name="blobName"></param>
/// <returns></returns>
public bool DoesBlobExists(string containerName, string blobName)
{
var exists = false;

return exists;
}

/// <summary>
/// Move blob
/// </summary>
Expand Down
8 changes: 8 additions & 0 deletions azurecopy/Handlers/IBlobHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ namespace azurecopy
/// </summary>
public interface IBlobHandler
{
/// <summary>
/// Does blob exists in container
/// </summary>
/// <param name="containerName"></param>
/// <param name="blobName"></param>
/// <returns></returns>
bool DoesBlobExists(string containerName, string blobName);

/// <summary>
/// Make container/directory (depending on platform).
/// </summary>
Expand Down
13 changes: 13 additions & 0 deletions azurecopy/Handlers/RackspaceHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ public RackspaceHandler(string url )

}

/// <summary>
/// Does blob exists in container
/// </summary>
/// <param name="containerName"></param>
/// <param name="blobName"></param>
/// <returns></returns>
public bool DoesBlobExists(string containerName, string blobName)
{
var exists = false;

return exists;
}

/// <summary>
/// Make container/directory (depending on platform).
/// </summary>
Expand Down
13 changes: 13 additions & 0 deletions azurecopy/Handlers/S3Handler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,19 @@ public string GetBlobNameFromUrl(string url)
return string.Join("/",sp.Skip(4));
}

/// <summary>
/// Does blob exists in container
/// </summary>
/// <param name="containerName"></param>
/// <param name="blobName"></param>
/// <returns></returns>
public bool DoesBlobExists(string containerName, string blobName)
{
var exists = false;

return exists;
}

// override configuration.
public void OverrideConfiguration( Dictionary<string,string> configuration)
{
Expand Down
12 changes: 12 additions & 0 deletions azurecopy/Handlers/SkyDriveHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ public string GetContainerNameFromUrl(string url)
}
}

/// <summary>
/// Does blob exists in container
/// </summary>
/// <param name="containerName"></param>
/// <param name="blobName"></param>
/// <returns></returns>
public bool DoesBlobExists(string containerName, string blobName)
{
var exists = false;

return exists;
}

/// <summary>
/// Gets blob name from the full url.
Expand Down
4 changes: 2 additions & 2 deletions azurecopy/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.3.3")]
[assembly: AssemblyFileVersion("1.3.3")]
[assembly: AssemblyVersion("1.4.0")]
[assembly: AssemblyFileVersion("1.4.0")]
70 changes: 48 additions & 22 deletions azurecopycommand/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class Program
-mc <full url> : Make container/folder/directory.
-configonedrive : Steps through configuring of OneDrive and saves config file with new data.
-configdropbox : Steps through configuring of Dropbox and saves config file with new data.
-skip: Skip blob if blob with same name exists at destination. Currently only valid when target is Azure Blob Storage.
Note: Remember when local file system is destination/output do NOT end the directory with a \
When destination is Onedrive, use the url format one://directory/file";
Expand All @@ -107,6 +108,7 @@ class Program
const string RetryAttemptDelayInSecondsFlag = "-rd";
const string MaxRetryAttemptsFlag = "-mr";
const string MakeContainerFlag = "-mc";
const string SkipFlag = "-skip";

// only makes sense for azure destination.
const string DestBlobType = "-destblobtype";
Expand Down Expand Up @@ -160,6 +162,7 @@ class Program
static bool _listContainer = false;
static bool _makeContainer = false;
static bool DebugMode = false;
static bool skip = false;

// destination blob... can only assign if source is NOT azure and destination IS azure.
static DestinationBlobType _destinationBlobType = DestinationBlobType.Unknown;
Expand Down Expand Up @@ -333,6 +336,9 @@ static void ParseArguments(string[] args)

break;

case SkipFlag:
skip = true;
break;

case SharepointUsernameFlag:
case SharepointUsernameShortFlag:
Expand Down Expand Up @@ -600,6 +606,13 @@ static void DoNormalCopy(bool debugMode)
IBlobHandler inputHandler = GetHandler(_inputUrlType, _inputUrl);
IBlobHandler outputHandler = GetHandler(_outputUrlType, _outputUrl);

// can only use skip when destination is Azure (simply because I haven't coded other options yet).
if (_outputUrlType != UrlType.Azure && skip)
{
Console.WriteLine("-skip option only valid when destination is Azure blob storage. This will change soon.");
return;
}

if (inputHandler != null && outputHandler != null)
{
// handle multiple files.
Expand All @@ -621,25 +634,7 @@ static void DoNormalCopy(bool debugMode)
}

var sourceContainer = inputHandler.GetContainerNameFromUrl(_inputUrl);

// read blob
var inputBlob = inputHandler.ReadBlob(sourceContainer, blob.Name, fileName);

// if blob is marked with type "Unknown" then set it to what was passed in on command line.
// if nothing was passed in, then default to block?
if (inputBlob.BlobType == DestinationBlobType.Unknown)
{
if (_destinationBlobType != DestinationBlobType.Unknown)
{
inputBlob.BlobType = _destinationBlobType;
}
else
{
inputBlob.BlobType = DestinationBlobType.Block;
}
}

Console.WriteLine(string.Format("Copying blob to {0}", _outputUrl));


// write blob
var destContainerName = outputHandler.GetContainerNameFromUrl(_outputUrl);
Expand All @@ -649,23 +644,23 @@ static void DoNormalCopy(bool debugMode)

if (!string.IsNullOrEmpty(blob.BlobPrefix))
{
destBlobName += inputBlob.Name.Substring(blob.BlobPrefix.Length);
destBlobName += blob.Name.Substring(blob.BlobPrefix.Length);
}
else
{
// if destBlobName ends with / then its a prefix.
// if it does not, then its an absolute blob name (ie when the blob was copied then it was also being renamed).
if (destBlobName.EndsWith("/"))
{
destBlobName += inputBlob.Name;
destBlobName += blob.Name;
}
else
{
// if destBlobName is empty then take it from blob.
// else it appears we were told the name to use so leave it.
if (string.IsNullOrWhiteSpace(destBlobName))
{
destBlobName = inputBlob.Name;
destBlobName = blob.Name;
}
else
{
Expand All @@ -674,6 +669,37 @@ static void DoNormalCopy(bool debugMode)
}
}

if (skip)
{
// check if destination blob exists.
// if it does, then dont even bother reading the blob and just continue to the next one.

var alreadyExists = outputHandler.DoesBlobExists(destContainerName, destBlobName);
if (alreadyExists)
{
Console.WriteLine("Skipping " + blob.Url);
continue;
}
}

Console.WriteLine(string.Format("Copying blob to {0}", _outputUrl));

// read blob
var inputBlob = inputHandler.ReadBlob(sourceContainer, blob.Name, fileName);
// if blob is marked with type "Unknown" then set it to what was passed in on command line.
// if nothing was passed in, then default to block?
if (inputBlob.BlobType == DestinationBlobType.Unknown)
{
if (_destinationBlobType != DestinationBlobType.Unknown)
{
inputBlob.BlobType = _destinationBlobType;
}
else
{
inputBlob.BlobType = DestinationBlobType.Block;
}
}

outputHandler.WriteBlob(destContainerName, destBlobName, inputBlob, ConfigHelper.ParallelFactor, ConfigHelper.ChunkSizeInMB);

if (inputBlob.BlobSavedToFile)
Expand Down
4 changes: 2 additions & 2 deletions azurecopycommand/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.3.3")]
[assembly: AssemblyFileVersion("1.3.3")]
[assembly: AssemblyVersion("1.4.0")]
[assembly: AssemblyFileVersion("1.4.0")]

0 comments on commit bec00f7

Please sign in to comment.