/// <summary>Sets last write time and creation time to S3 object.</summary> private void SetLastWriteTimeAndCreationTimeToS3Object() { string dateTimeString = S3ObjectInfoProvider.GetDateTimeString(DateTime.Now); if (this.obj.GetMetadata(S3ObjectInfoProvider.CREATION_TIME) == null) { this.obj.SetMetadata(S3ObjectInfoProvider.CREATION_TIME, dateTimeString, false); } this.obj.SetMetadata(S3ObjectInfoProvider.LAST_WRITE_TIME, dateTimeString); }
/// <summary> /// Opens a file, appends the specified string to the file, and then closes the file. If the file does not exist, this method creates a file, writes the specified string to the file, then closes the file. /// </summary> /// <param name="path">Path</param> /// <param name="contents">Content to write.</param> public override void AppendAllText(string path, string contents) { string directoryName = CMS.IO.Path.GetDirectoryName(path); if (!CMS.IO.Directory.Exists(directoryName)) { throw GetDirectoryNotFoundException(directoryName); } IS3ObjectInfo info = S3ObjectFactory.GetInfo(path); File.Provider.AppendTextToObject(info, contents); info.SetMetadata(S3ObjectInfoProvider.LAST_WRITE_TIME, S3ObjectInfoProvider.GetDateTimeString(DateTime.Now)); }
/// <summary> /// Creates a new file, writes the specified byte array to the file, and then closes the file. If the target file already exists, it is overwritten. /// </summary> /// <param name="path">Path to file.</param> /// <param name="bytes">Bytes to write.</param> public override void WriteAllBytes(string path, byte[] bytes) { string directoryName = CMS.IO.Path.GetDirectoryName(path); if (!CMS.IO.Directory.Exists(directoryName)) { throw GetDirectoryNotFoundException(directoryName); } var memoryStream = new System.IO.MemoryStream(bytes); IS3ObjectInfo info = S3ObjectFactory.GetInfo(path); Provider.PutDataFromStreamToObject(info, memoryStream); info.SetMetadata(S3ObjectInfoProvider.LAST_WRITE_TIME, S3ObjectInfoProvider.GetDateTimeString(DateTime.Now)); }
/// <summary> /// Sets the date and time that the specified file was last written to. /// </summary> /// <param name="path">Path to file.</param> /// <param name="lastWriteTime">Last write time.</param> public override void SetLastWriteTime(string path, DateTime lastWriteTime) { if (!this.Exists(path)) { throw GetFileNotFoundException(path); } IS3ObjectInfo info = S3ObjectFactory.GetInfo(path); if (Provider.ObjectExists(info)) { info.SetMetadata(S3ObjectInfoProvider.LAST_WRITE_TIME, S3ObjectInfoProvider.GetDateTimeString(lastWriteTime)); } else { throw new InvalidOperationException($"Cannot last write time to file '{path}' because is located only in application file system. \r\n This exception typically occurs when file system is mapped to Amazon S3 storage after the file or directory\r\n '{path}' was created in the local file system. To fix this issue move given file to Amazon S3 storage."); } }
/// <summary> /// Sets the specified FileAttributes of the file on the specified path. /// </summary> /// <param name="path">Path to file.</param> /// <param name="fileAttributes">File attributes.</param> public override void SetAttributes(string path, CMS.IO.FileAttributes fileAttributes) { if (!this.Exists(path)) { throw GetFileNotFoundException(path); } IS3ObjectInfo info = S3ObjectFactory.GetInfo(path); if (File.Provider.ObjectExists(info)) { info.SetMetadata(S3ObjectInfoProvider.ATTRIBUTES, ValidationHelper.GetString(ValidationHelper.GetInteger(fileAttributes, 0), string.Empty), false); info.SetMetadata(S3ObjectInfoProvider.LAST_WRITE_TIME, S3ObjectInfoProvider.GetDateTimeString(DateTime.Now)); } else { throw new InvalidOperationException($"Cannot set attributes to file '{path}' because it exists only in application file system. \r\n This exception typically occurs when file system is mapped to Amazon S3 storage after the file or directory\r\n '{path}' was created in the local file system. To fix this issue move given file to Amazon S3 storage."); } }
/// <summary> /// Copies an existing file to a new file. Overwriting a file of the same name is allowed. /// </summary> /// <param name="sourceFileName">Path to source file.</param> /// <param name="destFileName">Path to destination file.</param> /// <param name="overwrite">If destination file should be overwritten.</param> public override void Copy(string sourceFileName, string destFileName, bool overwrite) { if (!this.Exists(sourceFileName)) { throw GetFileNotFoundException(sourceFileName); } bool destExists = CMS.IO.File.Exists(destFileName); if (destExists && !overwrite) { return; } if (!StorageHelper.IsSameStorageProvider(sourceFileName, destFileName)) { StorageHelper.CopyFileAcrossProviders(sourceFileName, destFileName); } else { IS3ObjectInfo sourceInfo = S3ObjectFactory.GetInfo(sourceFileName); IS3ObjectInfo destInfo = S3ObjectFactory.GetInfo(destFileName); if (destExists) { Provider.DeleteObject(destInfo); } if (Provider.ObjectExists(sourceInfo)) { Provider.CopyObjects(sourceInfo, destInfo); } else { Provider.PutFileToObject(destInfo, sourceFileName); } IS3ObjectInfo destDirectoryInfo = S3ObjectFactory.GetInfo(CMS.IO.Path.GetDirectoryName(destFileName)); destDirectoryInfo.Key = $"{destDirectoryInfo.Key}/"; if (!Provider.ObjectExists(destDirectoryInfo)) { Provider.CreateEmptyObject(destDirectoryInfo); } var now = DateTime.Now; destDirectoryInfo.SetMetadata(S3ObjectInfoProvider.LAST_WRITE_TIME, S3ObjectInfoProvider.GetDateTimeString(now)); destInfo.SetMetadata(S3ObjectInfoProvider.LAST_WRITE_TIME, S3ObjectInfoProvider.GetDateTimeString(now), false); destInfo.SetMetadata(S3ObjectInfoProvider.CREATION_TIME, S3ObjectInfoProvider.GetDateTimeString(now)); } }
/// <summary> /// Deletes the specified file. An exception is not thrown if the specified file does not exist. /// </summary> /// <param name="path">Path to file</param> public override void Delete(string path) { if (!this.Exists(path)) { throw GetFileNotFoundException(path); } IS3ObjectInfo info1 = S3ObjectFactory.GetInfo(path); if (Provider.ObjectExists(info1)) { Provider.DeleteObject(info1); IS3ObjectInfo info2 = S3ObjectFactory.GetInfo(CMS.IO.Path.GetDirectoryName(path)); info2.Key = $"{info2.Key}/"; if (!Provider.ObjectExists(info2)) { Provider.CreateEmptyObject(info2); } info2.SetMetadata(S3ObjectInfoProvider.LAST_WRITE_TIME, S3ObjectInfoProvider.GetDateTimeString(DateTime.Now)); } else { throw new InvalidOperationException($"File '{path}' cannot be deleted because it exists only in application file system. \r\n This exception typically occurs when file system is mapped to Amazon S3 storage after the file or directory\r\n '{path}' was created in the local file system. To fix this issue remove specified file or directory."); } }