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

Bucket prefix issue fixed #15

Open
wants to merge 1 commit into
base: v8-master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ private BucketFileSystemConfig CreateConfiguration()
var bucketName = ConfigurationManager.AppSettings[$"{AppSettingsKey}:BucketName"];
var bucketHostName = ConfigurationManager.AppSettings[$"{AppSettingsKey}:BucketHostname"];
var bucketPrefix = ConfigurationManager.AppSettings[$"{AppSettingsKey}:MediaPrefix"];
var mediaPrefix = ConfigurationManager.AppSettings[$"{AppSettingsKey}:MediaFolderName"];
var region = ConfigurationManager.AppSettings[$"{AppSettingsKey}:Region"];
bool.TryParse(ConfigurationManager.AppSettings[$"{AppSettingsKey}:DisableVirtualPathProvider"], out var disableVirtualPathProvider);

Expand All @@ -67,7 +68,8 @@ private BucketFileSystemConfig CreateConfiguration()
Region = region,
CannedACL = new S3CannedACL("public-read"),
ServerSideEncryptionMethod = "",
DisableVirtualPathProvider = disableVirtualPathProvider
DisableVirtualPathProvider = disableVirtualPathProvider,
MediaPrefix = mediaPrefix
};
}
}
Expand Down
53 changes: 26 additions & 27 deletions Umbraco.Storage.S3/Umbraco.Storage.S3/BucketFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,32 +70,31 @@ protected virtual IEnumerable<ListObjectsResponse> ExecuteWithContinuation(ListO
}
}

protected virtual string ResolveBucketPath(string path, bool isDir = false)

protected virtual string ResolveMediaPath(string path, bool isDir = false)
{
if (string.IsNullOrEmpty(path))
return Config.BucketPrefix;
return Config.MediaPrefix;

//Remove Bucket Hostname
if (!path.Equals("/") && path.StartsWith(Config.BucketHostName, StringComparison.InvariantCultureIgnoreCase))
path = path.Substring(Config.BucketHostName.Length);
if (!path.Equals("/") && path.StartsWith(Config.MediaPrefix, StringComparison.InvariantCultureIgnoreCase))
path = path.Substring(Config.MediaPrefix.Length);

// Equalise delimiters
path = path.Replace("/", Delimiter).Replace("\\", Delimiter);
path = path.Replace("\\", Delimiter);
if (path == Delimiter)
return "";

if (path.StartsWith(Delimiter))
path = path.Substring(1);

//Remove Key Prefix If Duplicate
if (path.StartsWith(Config.BucketPrefix, StringComparison.InvariantCultureIgnoreCase))
path = path.Substring(Config.BucketPrefix.Length);
if (path.StartsWith(Config.MediaPrefix, StringComparison.InvariantCultureIgnoreCase))
path = path.Substring(Config.MediaPrefix.Length);

if (isDir && !path.EndsWith(Delimiter))
path = string.Concat(path, Delimiter);
if (isDir && (!path.EndsWith(Delimiter)))
path = string.Concat(Delimiter, path);

if (path.StartsWith(Delimiter))
path = path.Substring(1);

return string.Concat(Config.BucketPrefix, "/", path);
return path;
}

protected virtual string RemovePrefix(string key)
Expand All @@ -111,7 +110,7 @@ public virtual IEnumerable<string> GetDirectories(string path)
if (string.IsNullOrEmpty(path))
path = "/";

path = ResolveBucketPath(path, true);
path = string.Concat(Config.BucketPrefix, ResolveMediaPath(path, true));
var request = new ListObjectsRequest
{
BucketName = Config.BucketName,
Expand All @@ -137,7 +136,7 @@ public virtual void DeleteDirectory(string path, bool recursive)
var listRequest = new ListObjectsRequest
{
BucketName = Config.BucketName,
Prefix = ResolveBucketPath(path, true)
Prefix = string.Concat(Config.BucketPrefix, ResolveMediaPath(path, true))
};

var listResponse = ExecuteWithContinuation(listRequest);
Expand All @@ -163,7 +162,7 @@ public virtual bool DirectoryExists(string path)
var request = new ListObjectsRequest
{
BucketName = Config.BucketName,
Prefix = ResolveBucketPath(path, true),
Prefix = string.Concat(Config.BucketPrefix, ResolveMediaPath(path, true)),
MaxKeys = 1
};

Expand All @@ -184,7 +183,7 @@ public virtual void AddFile(string path, Stream stream, bool overrideIfExists)
var request = new PutObjectRequest
{
BucketName = Config.BucketName,
Key = ResolveBucketPath(path),
Key = string.Concat(Config.BucketPrefix, ResolveMediaPath(path, true)),
CannedACL = Config.CannedACL,
ContentType = MimeTypeResolver.Resolve(path),
InputStream = memoryStream,
Expand All @@ -208,7 +207,7 @@ public virtual IEnumerable<string> GetFiles(string path)

public virtual IEnumerable<string> GetFiles(string path, string filter)
{
path = ResolveBucketPath(path, true);
path = string.Concat(Config.BucketPrefix, ResolveMediaPath(path, true));

string filename = Path.GetFileNameWithoutExtension(filter);
if (filename.EndsWith("*"))
Expand Down Expand Up @@ -246,7 +245,7 @@ public virtual Stream OpenFile(string path)
var request = new GetObjectRequest
{
BucketName = Config.BucketName,
Key = ResolveBucketPath(path)
Key = string.Concat(Config.BucketPrefix, ResolveMediaPath(path, true))
};

MemoryStream stream;
Expand All @@ -270,7 +269,7 @@ public virtual void DeleteFile(string path)
var request = new DeleteObjectRequest
{
BucketName = Config.BucketName,
Key = ResolveBucketPath(path)
Key = string.Concat(Config.BucketPrefix, ResolveMediaPath(path, true))
};
Execute(client => client.DeleteObject(request));
}
Expand All @@ -283,7 +282,7 @@ public virtual bool FileExists(string path)
var request = new GetObjectMetadataRequest
{
BucketName = Config.BucketName,
Key = ResolveBucketPath(path)
Key = string.Concat(Config.BucketPrefix, ResolveMediaPath(path, true))
};

try
Expand Down Expand Up @@ -323,9 +322,9 @@ public virtual string GetRelativePath(string fullPathOrUrl)
}

//Strip Bucket Prefix
if (fullPathOrUrl.StartsWith(Config.BucketPrefix, StringComparison.InvariantCultureIgnoreCase))
if (fullPathOrUrl.StartsWith(Config.MediaPrefix, StringComparison.InvariantCultureIgnoreCase))
{
fullPathOrUrl = fullPathOrUrl.Substring(Config.BucketPrefix.Length);
fullPathOrUrl = fullPathOrUrl.Substring(Config.MediaPrefix.Length);
fullPathOrUrl = fullPathOrUrl.TrimStart(Delimiter.ToCharArray());
}

Expand All @@ -351,15 +350,15 @@ public virtual string GetUrl(string path)
hostName = "";
}

return string.Concat(hostName, "/", ResolveBucketPath(path));
return string.Concat(hostName, "/", Config.MediaPrefix, ResolveMediaPath(path, true));
}

public virtual DateTimeOffset GetLastModified(string path)
{
var request = new GetObjectMetadataRequest
{
BucketName = Config.BucketName,
Key = ResolveBucketPath(path)
Key = string.Concat(Config.BucketPrefix, ResolveMediaPath(path, true))
};

var response = Execute(client => client.GetObjectMetadata(request));
Expand All @@ -378,7 +377,7 @@ public long GetSize(string path)
var request = new GetObjectMetadataRequest
{
BucketName = Config.BucketName,
Key = ResolveBucketPath(path)
Key = string.Concat(Config.BucketPrefix, ResolveMediaPath(path, true))
};

var response = Execute(client => client.GetObjectMetadata(request));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public class BucketFileSystemConfig

public string BucketPrefix { get; set; }

public string MediaPrefix { get; set; }

public string Region { get; set; }

public S3CannedACL CannedACL { get; set; }
Expand Down