Microsoft Azure PlayFab logo
    • Multiplayer
    • LiveOps
    • Data & Analytics
    • Add-ons
    • For Any Role

      • Engineer
      • Designer
      • Executive
      • Marketer
    • For Any Stage

      • Build
      • Improve
      • Grow
    • For Any Size

      • Solo
      • Indie
      • AAA
  • Runs on PlayFab
  • Pricing
    • Blog
    • Forums
    • Contact us
  • Sign up
  • Sign in
  • Ask a question
  • Spaces
    • PlayStream
    • Feature Requests
    • Add-on Marketplace
    • Bugs
    • API and SDK Questions
    • General Discussion
    • LiveOps
    • Topics
    • Questions
    • Articles
    • Ideas
    • Users
    • Badges
  • Home /
  • API and SDK Questions /
avatar image
Question by lmo · Sep 10, 2018 at 05:32 PM · apissdksContent

CDN files upload with nodejs script and https

Hi, I have been trying to upload files by using Nodejs as an alternative to the sdk for untiy which do not work for command line. I did successfully loaded the content from the editor but I need to upload the files from an automatic job for continuous delivery purposes and since the web request do not work by executing untiy in batch mode I need an alternative.

I'm able to authenticate and to get the upload url but when I try to upload a file I always get 501 as an answer.

I have tried different libraries:

https (default included in nodejs)

request.js

postman-request.js

The three of the return 501.

The following code is by using

https

var playfab = require("playfab-sdk");
var request = require("request");
var fs = require("fs");
const url = require('url');
const https = require('https');

function UploadAssets() {
    console.log("Uploading assets started");
    
    playfab.PlayFab.settings.titleId = "xxxx";
    playfab.PlayFab.settings.developerSecretKey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX";

    let uploadUrlRequest = {
        "Key": "0.0.0/Andoid/AssetBundlesCollection.json",
        "ContentType": "binary/octet-stream"
    };

    playfab.PlayFabAdmin.GetContentUploadUrl(uploadUrlRequest, (error, result) => {
        if(!error)
        {
            console.log(result.data.URL);
            let resultUri = url.parse(result.data.URL);
            console.log("host = " + resultUri.host);
            console.log("path = " + resultUri.path);
            
            let collection = "D:/Repos/foo/Assets/AssetBundlesCollection.json";


            // https way
            fs.createReadStream(collection).pipe( https.request({
                host: resultUri.host,
                path: resultUri.path,
                method: 'PUT',
                encoding: null,
                headers:{ 
                    'Content-Type' : "binary/octet-stream"
                }
            }, 
            (res) => {
                console.log('STATUS:', res.statusCode);
                console.log('HEADERS:', JSON.stringify(res.headers));
        
                res.setEncoding('utf8');
        
                res.on('data', function (chunk) {
                    console.log('BODY:', chunk);
                });
        
                res.on('end', function () {
                    console.log('No more data in response.');
                })
            }));
        }
    });

    
}

UploadAssets();

I tested by using POSTMAN direclty as described here:

https://api.playfab.com/docs/tutorials/execute-playfab-api-via-postman

I was able to upload the file succesfuly but I'm not able to see any difference between my request with https donde by node.js and the one done by postman, even postman gives node.js code based on the request and it doesn't work, I get the same error.

var playfab = require("playfab-sdk");
var request = require("request");
var fs = require("fs");
const url = require('url');
const https = require('https');
const querystring = require('querystring');

function UploadAssets() {
    console.log("Uploading assets started");
    
    playfab.PlayFab.settings.titleId = "xxxx";
    playfab.PlayFab.settings.developerSecretKey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX";

    let uploadUrlRequest = {
        "Key": "0.0.0/Andoid/AssetBundlesCollection.json",
        "ContentType": "binary/octet-stream"
    };

    playfab.PlayFabAdmin.GetContentUploadUrl(uploadUrlRequest, (error, result) => {
        if(!error)
        {
            console.log(result.data.URL);
            let resultUri = url.parse(result.data.URL);
            let query = querystring.parse(resultUri.query);
            console.log("protocol = " + resultUri.protocol + "\n");
            console.log("\nhost = " + resultUri.host + "\n");
            console.log("pathname = " + resultUri.pathname + "\n");
            console.log("path = " + resultUri.path + "\n");
            console.log("qs = " + resultUri.query + "\n");
            
            let collection = "D:/Repos/foo/Assets/AssetBundlesCollection.json";

           // postman https suggestion
           var options = { method: 'PUT',
            url: resultUri.protocol + "//" + resultUri.host + resultUri.pathname,
            qs: query,
            headers: { 'content-type': 'binary/octet-stream' } };

            console.log(JSON.stringify(options, null, 2));

            fs.createReadStream(collection).pipe(request(options, function (error, response, body) {
                if (error) throw new Error(error);
                console.log(body);
            }));
        }
    });

    
}

UploadAssets();
Comment

People who like this

0 Show 0
10 |1200 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by lmo · Sep 11, 2018 at 04:49 PM

I found the solution, I found out with postman desktop application that the content-length, accept and cache-control headers where being sent, with them it works perfect. For some strange reason the postman chrome extension do not show those headers.

let UploadFile = (filepath, remotepath) =>
{
    console.log("Uploading " + filepath + " to " + remotepath);
    let uploadUrlRequest = {
        "Key": remotepath,
        "ContentType": "binary/octet-stream"
    };

    playfab.PlayFabAdmin.GetContentUploadUrl(uploadUrlRequest, (error, result) => {
        if(!error)
        {
            let resultUri = url.parse(result.data.URL);
            let query = querystring.parse(resultUri.query);
            
            let binarybuf = fs.readFileSync(filepath, null);

            var options = { method: 'PUT',
                    url: resultUri.protocol + "//" + resultUri.host + resultUri.pathname,
                    qs: query,
                    body: binarybuf,
                    encoding: null,
                    headers: { 
                        'content-type': 'binary/octet-stream',
                        'cache-control': 'no-cache',
                        'accept' : '*/*',
                        'content-length' : binarybuf.byteLength
                    }
            };

            request(options, function (error, response, body) {
                if (error) throw new Error(error);
                console.log(  resultUri.pathname + " / " + response.statusCode + " / " +response.statusMessage);
            });
        }
    });
}

Comment

People who like this

0 Show 0 · Share
10 |1200 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image

Answer by Andy · Sep 10, 2018 at 09:40 PM

One caveat, I've never tried to do this from node.js. That being said, I'm a little curious about the url parameter in your options object. Based on the latest node.js api docs, it doesn't look like that's a valid option. It does look like https.request will also accept a URL object directly, but it shouldn't be wrapped inside of an options object.

Hopefully that's helpful. Good Luck!

Comment

People who like this

0 Show 0 · Share
10 |1200 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Navigation

Spaces
  • General Discussion
  • API and SDK Questions
  • Feature Requests
  • PlayStream
  • Bugs
  • Add-on Marketplace
  • LiveOps
  • Follow this Question

    Answers Answers and Comments

    2 People are following this question.

    avatar image avatar image

    Related Questions

    Display Properties for User Generated Content? 1 Answer

    How to download content from cdn? 1 Answer

    Having trouble with Get content from CDN to Update my game's version in Unreal Engine 4 0 Answers

    Java API - Stupid question 1 Answer

    How to Upload and Download a file with the C++ SDK? 2 Answers

    PlayFab

    • Multiplayer
    • LiveOps
    • Data & Analytics
    • Runs on PlayFab
    • Pricing

    Solutions

    • For Any Role

      • Engineer
      • Designer
      • Executive
      • Marketer
    • For Any Stage

      • Build
      • Improve
      • Grow
    • For Any Size

      • Solo
      • Indie
      • AAA

    Engineers

    • Documentation
    • Quickstarts
    • API Reference
    • SDKs
    • Usage Limits

    Resources

    • Forums
    • Contact us
    • Blog
    • Service Health
    • Terms of Service
    • Attribution

    Follow us

    • Facebook
    • Twitter
    • LinkedIn
    • YouTube
    • Sitemap
    • Contact Microsoft
    • Privacy & cookies
    • Terms of use
    • Trademarks
    • Safety & eco
    • About our ads
    • © Microsoft 2020
    • Anonymous
    • Sign in
    • Create
    • Ask a question
    • Create an article
    • Post an idea
    • Spaces
    • PlayStream
    • Feature Requests
    • Add-on Marketplace
    • Bugs
    • API and SDK Questions
    • General Discussion
    • LiveOps
    • Explore
    • Topics
    • Questions
    • Articles
    • Ideas
    • Users
    • Badges