简体   繁体   English

AWS S3中的物理文件访问

[英]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: 您可以做的几件事:

  1. Map the S3 folder to a drive on the machine 将S3文件夹映射到计算机上的驱动器

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");
  1. You can create an abstraction layer while accessing files. 您可以在访问文件时创建抽象层。 I recently worked on something like that in a project. 我最近在一个项目中从事类似的工作。 What I did was access the objects through a common interface. 我所做的是通过一个通用接口访问对象。 You can have a look at the code snippet below. 您可以查看下面的代码段。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM