简体   繁体   中英

ServiceStack - upload files as byte stream?

I am trying to create my own S3 compatible server using self-hosted ServiceStack (version 4.0.31 on mono) and I need to support s3curl.pl format uploads. The problem is that they do not use mutipart uploads but appear to use the default http binary format. I already have ServiceStack working for multipart/form-data uploads. I have also looked at all applicable posts on related topics and none of them work for me. For example: ServiceStack - Upload files with stream and with Uri looked like it would address my problem. The problem is that I can't resolve RequestContext.Files for that solution.

Here is my curl command line (note that the Auth is bogus so it is not a security risk):

curl -H 'Date: Thu, 09 Oct 2014 16:56:21 +0000' -H 'Authorization: AWS Y29tZXQ=:ciitxdxRvWrlcVsc48JnXqKu/oY=' -L -H 'content-type: ' -T README.txt 'http://localhost:1301/s3/README.txt?format=xml' -k

The base.Request.Files API is only for handling HTTP multipart/form-data file uploads.

To handle other File Uploads you can decorate your Request DTO with IRequiresRequestStream which tells ServiceStack to skip deserialization and inject the Raw InputStream into your DTO instead:

public class RawFileUpload : IRequiresRequestStream
{
    public Stream RequestStream { get; set; }
}

In your Service you can handle

public class UploadServices : Service
{
    public object Any(RawFileUpload request)
    {
        var filePath = "~/uploads/{0}".Fmt(Guid.NewGuid().ToString("n"))
                       .MapAbsolutePath();
        using (var fs = File.OpenWrite(filePath))
        {
            request.RequestStream.WriteTo(fs);
        }
        //...
    }
}

This feature is described in the Serialization/Deserialization wiki .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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