Skip to content

Chunked Media Upload Tips

Troy Willmot edited this page Sep 16, 2016 · 2 revisions

In order to save you from losing hours of your life trying to upload chunked media to Twitter, like I did, please do the following;

  • Read the Twitter Media Upload Guide. This will give you a basic idea of the flow and what's required.
  • Disabuse yourself of the notion that you know enough to successfully upload chunked media.
  • Make sure when you calling the UploadMediaInit API on the TweetSharp service.
  • Ensure you upload each chunk in serial (if retries are required, make sure each chunk is retried until success before moving on to the next). Do not try to upload chunks in parallel.
  • Even though the Finalize and Status methods/endpoints return the same data, do not assume you can call the Finalize method to get the current status after async processing has started. You must call Finalize once, then call status on subsequent calls, if required.
  • Make sure your media meets the requirements specified by Twitter. There is nothing TweetSharp can do for you if it doesn't, though maybe you could implement a transcoding/transform yourself.
  • See the Can_Upload_VideoChunked or Can_Upload_VideoChunkedAsync test methods in the TweetMoaSharp repository for (rough) sample code that works if you're having trouble.

A word about the MediaCategory value provided to the Init method; A known set of allowed values is exposed via the TwitterMediaCategory, as constants (not an enum for technical reasons I'm not going into now).

  • Use TwitterMediaCategory.Image for any still image including gifs.
  • Use TwitterMediaCategory.AnimatedGif for any animated gif. Yes, your code will need to determine for itself what kind of gif the user has provided.
  • Use TwitterMediaCategory.Video for any kind of supported video (but not animated gifs).

Technically the MediaCategory parameter is optional, however if you do not provide it and the media requires async processing by Twitter (quite likely for chunked uploads), then the finalize method will return a mostly unhelpful error. In this case the async processing will not take place, and the status method will not return anything useful either. For this reason I recommend always providing the media category.

Handling Problems
All of this was pieced together from the Twitter developer forums. If you're getting errors back from Twitter saying the media is invalid or the async processing doesn't complete etc then you need to go and ask there. We can only (potentially) help in cases where TweetSharp is doing the wrong thing.

Check the unit tests are working, if they are, try those unit tests with your media, if that doesn't work then either your media is incompatible or there's some other magic undocumented thing about the API we don't know. We're happy to learn if you know what's missing, but we have limited time and aren't going to research every piece of media that doesn't work for someone.

Why do I have to do this myself, why isn't there a single method to upload a file in chunks?
Many reasons. Three primary ones;

  1. "TweetSharp is a fast, clean wrapper around the Twitter API." - this the description of the library. It is accurate. TweetSharp exposes the Twitter API in a way that is easy to access/use from .Net. It does not generally provide additional logic on top of the Twitter API, by design.

  2. We didn't have time, and if you're using this library then you're a programmer, so writing some code shouldn't be too much of a hardship. Given the unit tests can be used as a sample, it's even easier.

  3. A method provided by TweetSharp would likely do the wrong thing. Under what conditions do you want to retry a chunk? How many times? Do you want a timeout on the whole operation? On individual chunks? How big should the chunks be? Libraries like Polly exist and are excellent for handling many of these things, but we do not want to take a direct dependency on it, and without it we're rebuilding a bunch of logic in a (probably) worse way. We'd also have to take a multitude of parameters to know how to handle all the edge cases. This is why you should write the code yourself, for your environment/media/networking conditions etc.

Clone this wiki locally