[英]Physical file access in AWS S3
I would like to move some local application into the Amazon cloud which internally works on the FileInfo
to process some files. 我想将一些本地应用程序移到内部在
FileInfo
上处理某些文件的Amazon云中。
I know about the bucket concept as well as the file adaption using S3FileInfo
which is pointing to the stored files within the buckets. 我了解存储桶的概念以及使用
S3FileInfo
的文件适配,该文件指向存储在存储桶中的文件。
But since the application is using FileInfo
I was wondering if there is any chance I can somehow create a FileInfo
instance pointing to a file within buckets? 但是由于应用程序正在使用
FileInfo
所以我想知道是否有可能以某种方式创建指向存储桶中文件的FileInfo
实例?
FileInfo class cannot be used to work with S3 objects directly. FileInfo类不能用于直接使用S3对象。 A couple of things you can do:
您可以做的几件事:
You can use a tool like TntDrive . 您可以使用TntDrive之类的工具。 Then you can access files you'd normally do, something like:
然后,您可以访问通常要做的文件,例如:
FileInfo file = new FileInfo(@"Z:\Path\myfile.doc");
This way file system dependency is encapsulated inside TextFilePersistanceProvider class only and the remaining application can work with any source. 这种文件系统依赖关系仅封装在TextFilePersistanceProvider类中,其余应用程序可以与任何源一起使用。
public interface IGamePersistanceProvider
{
void Save(Game game);
}
public class TextFilePersistanceProvider : IGamePersistanceProvider
{
private IConfigurationProvider _configurationProvider;
public TextFilePersistanceProvider(IConfigurationProvider configurationProvider)
{
_configurationProvider = configurationProvider;
}
public void Save(Game game)
{
if (game == null) { throw new ArgumentException("Unexpected parameter"); }
var filePath = _configurationProvider.GetValue<string>("LatestGamePath");
var gameAsJson = JsonConvert.SerializeObject(game, Formatting.None, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto });
File.WriteAllText(filePath, gameAsJson);
}
}
public class S3PersistanceProvider : IGamePersistanceProvider
{
private IConfigurationProvider _configurationProvider;
public S3PersistanceProvider(IConfigurationProvider configurationProvider)
{
_configurationProvider = configurationProvider;
}
public void Save(Game game)
{
if (game == null) { throw new ArgumentException("Unexpected parameter"); }
var gameAsJson = JsonConvert.SerializeObject(game, Formatting.None, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto,
});
string accessKey = _configurationProvider.GetValue<dynamic>("S3Credentials").AccessKey.ToString();
string secretKey = _configurationProvider.GetValue<dynamic>("S3Credentials").SecretKey.ToString();
string bucketName = _configurationProvider.GetValue<dynamic>("S3Credentials").BucketName.ToString();
string key = game.Name;
using (var s3Client = new AmazonS3Client(new BasicAWSCredentials(accessKey, secretKey), RegionEndpoint.EUWest1))
{
var transferUtil = new TransferUtility(s3Client);
var memStream = new MemoryStream(Encoding.UTF8.GetBytes(gameAsJson));
transferUtil.Upload(memStream, bucketName, key);
}
}
}
I hope this helps a bit. 我希望这个能有一点帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.