Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions src/SharpSync/SharpSync.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
Expand Down Expand Up @@ -38,8 +38,8 @@
<PackageReference Include="SQLitePCLRaw.bundle_e_sqlite3" Version="3.0.2" />
<PackageReference Include="WebDav.Client" Version="2.9.0" />
<PackageReference Include="SSH.NET" Version="2025.1.0" />
<PackageReference Include="AWSSDK.S3" Version="4.0.11.2" />
<PackageReference Include="FluentFTP" Version="53.0.2" />
<PackageReference Include="AWSSDK.S3" Version="3.7.*" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="9.0.0" PrivateAssets="all" />
</ItemGroup>
Expand Down
43 changes: 23 additions & 20 deletions src/SharpSync/Storage/S3Storage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,50 +207,51 @@ public async Task<IEnumerable<SyncItem>> ListItemsAsync(string path, Cancellatio
response = await _client.ListObjectsV2Async(request, cancellationToken);

// Add files (objects)
foreach (var s3Object in response.S3Objects) {
var s3Objects = response?.S3Objects ?? Enumerable.Empty<S3Object>();
foreach (var s3Object in s3Objects) {
cancellationToken.ThrowIfCancellationRequested();

// Skip directory marker objects (keys ending with '/')
if (s3Object.Key.EndsWith('/')) {
if (s3Object.Key is not null && s3Object.Key.EndsWith('/')) {
continue;
}

var relativePath = GetRelativePath(s3Object.Key);
var relativePath = GetRelativePath(s3Object.Key ?? string.Empty);

items.Add(new SyncItem {
Path = relativePath,
IsDirectory = false,
Size = s3Object.Size,
LastModified = s3Object.LastModified.ToUniversalTime(),
Size = s3Object.Size ?? 0,
LastModified = s3Object.LastModified?.ToUniversalTime() ?? DateTime.UtcNow,
ETag = s3Object.ETag?.Trim('"'),
MimeType = GetMimeType(s3Object.Key),
MimeType = GetMimeType(s3Object.Key ?? string.Empty),
Metadata = new Dictionary<string, object> {
["StorageClass"] = s3Object.StorageClass?.Value ?? "STANDARD"
}
});
}

// Add directories (common prefixes)
foreach (var commonPrefix in response.CommonPrefixes) {
var commonPrefixes = response?.CommonPrefixes ?? Enumerable.Empty<string>();
foreach (var commonPrefix in commonPrefixes) {
cancellationToken.ThrowIfCancellationRequested();

// CommonPrefix includes trailing slash, remove it for relative path
var relativePath = GetRelativePath(commonPrefix.TrimEnd('/'));

// Avoid duplicates using HashSet.Add's return value (CA1868)
if (directories.Add(relativePath)) {
items.Add(new SyncItem {
Path = relativePath,
IsDirectory = true,
Size = 0,
LastModified = DateTime.UtcNow, // S3 doesn't track directory timestamps
LastModified = DateTime.UtcNow,
MimeType = null
});
}
}

request.ContinuationToken = response.NextContinuationToken;
} while (response.IsTruncated);
request.ContinuationToken = response?.NextContinuationToken;
} while (response is not null && response.IsTruncated.GetValueOrDefault());

return (IEnumerable<SyncItem>)items;
}, cancellationToken);
Expand Down Expand Up @@ -279,7 +280,7 @@ public async Task<IEnumerable<SyncItem>> ListItemsAsync(string path, Cancellatio
Path = path,
IsDirectory = false,
Size = response.ContentLength,
LastModified = response.LastModified.ToUniversalTime(),
LastModified = response.LastModified?.ToUniversalTime() ?? DateTime.UtcNow,
ETag = response.ETag?.Trim('"'),
MimeType = response.Headers.ContentType,
Metadata = new Dictionary<string, object> {
Expand All @@ -296,7 +297,10 @@ public async Task<IEnumerable<SyncItem>> ListItemsAsync(string path, Cancellatio

var listResponse = await _client.ListObjectsV2Async(listRequest, cancellationToken);

if (listResponse.S3Objects.Count > 0 || listResponse.CommonPrefixes.Count > 0) {
var hasObjects = listResponse?.S3Objects is not null && listResponse.S3Objects.Count > 0;
var hasPrefixes = listResponse?.CommonPrefixes is not null && listResponse.CommonPrefixes.Count > 0;

if (hasObjects || hasPrefixes) {
return new SyncItem {
Path = path,
IsDirectory = true,
Expand Down Expand Up @@ -479,7 +483,7 @@ await ExecuteWithRetry(async () => {
// First, try to get the item to determine if it's a file or directory
var item = await GetItemAsync(path, cancellationToken);

if (item == null) {
if (item is null) {
return true; // Already deleted
}

Expand Down Expand Up @@ -514,19 +518,18 @@ private async Task DeleteDirectoryRecursive(string prefix, CancellationToken can
ListObjectsV2Response? response;
do {
response = await _client.ListObjectsV2Async(listRequest, cancellationToken);

if (response.S3Objects.Count > 0) {
// Delete objects in batches (S3 allows up to 1000 objects per request)
var objectsToDelete = response?.S3Objects ?? Enumerable.Empty<S3Object>();
if (objectsToDelete.Any()) {
var deleteRequest = new DeleteObjectsRequest {
BucketName = _bucketName,
Objects = response.S3Objects.Select(obj => new KeyVersion { Key = obj.Key }).ToList()
Objects = objectsToDelete.Select(obj => new KeyVersion { Key = obj.Key }).ToList()
};

await _client.DeleteObjectsAsync(deleteRequest, cancellationToken);
}

listRequest.ContinuationToken = response.NextContinuationToken;
} while (response.IsTruncated);
listRequest.ContinuationToken = response?.NextContinuationToken;
} while (response is not null && response.IsTruncated.GetValueOrDefault());

// Also delete the directory marker if it exists
try {
Expand Down
Loading