Пример #1
0
        /**
         * Example of how to create a new bucket using Forge SDK.
         * Uses the oauth2TwoLegged and twoLeggedCredentials objects that you retrieved previously.
         */
        private static void createBucket()
        {
            Console.WriteLine("***** Sending createBucket request");
            PostBucketsPayload payload  = new PostBucketsPayload(BUCKET_KEY, null, PostBucketsPayload.PolicyKeyEnum.Persistent);
            dynamic            response = bucketsApi.CreateBucket(payload, "US");

            Console.WriteLine("***** Response for createBucket: " + response.ToString());
        }
Пример #2
0
        public async Task UploadDataSetAsync()
        {
            dynamic oauth = await OAuthController.GetInternalAsync();

            var nickname = await GetNicknameAsync();

            string bucketKey = nickname.ToLower() + BucketKey;

            BucketsApi buckets = new BucketsApi();

            buckets.Configuration.AccessToken = oauth.access_token;
            try
            {
                var     postBuckets = new PostBucketsPayload(bucketKey, null, PostBucketsPayload.PolicyKeyEnum.Persistent);
                dynamic result      = buckets.CreateBucket(postBuckets);
                //Console.WriteLine("bucket: " + result.ToString());
            }
            catch (Exception)
            {
                // Most likely bucket already exists, we'll just skip this error and catch any real errors on upload
            }

            ObjectsApi objects = new ObjectsApi();

            objects.Configuration.AccessToken = oauth.access_token;
            var objectName    = ObjectName;                 // string | URL-encoded object name
            var contentLength = 56;                         // int? | Indicates the size of the request body.

            System.IO.Stream body = File.OpenRead(DataSet); /// path / to / file.txt;  // System.IO.Stream |

            try
            {
                var result = objects.UploadObject(bucketKey, objectName, contentLength, body);
            }
            catch (Exception e)
            {
                Console.WriteLine("Error uploading data set: " + e.Message);
            }
        }
Пример #3
0
        public ActionResult Upload(HttpPostedFileBase file)
        {
            if (file == null || file.ContentLength == 0)
            {
                return(RedirectToAction("Index"));
            }

            string fileName     = Path.GetFileName(file.FileName);
            string fileSavePath = Path.Combine(Server.MapPath("~/App_Data/"), fileName);

            file.SaveAs(fileSavePath);

            // get a write enabled token
            TwoLeggedApi oauthApi = new TwoLeggedApi();
            dynamic      bearer   = oauthApi.Authenticate(
                WebConfigurationManager.AppSettings["FORGE_CLIENT_ID"],
                WebConfigurationManager.AppSettings["FORGE_CLIENT_SECRET"],
                "client_credentials",
                new Scope[] { Scope.BucketCreate, Scope.DataCreate, Scope.DataWrite, Scope.DataRead });

            // create a randomg bucket name (fixed prefix + randomg guid)
            string bucketKey = "forgeapp" + Guid.NewGuid().ToString("N").ToLower();

            // create the Forge bucket
            PostBucketsPayload postBucket = new PostBucketsPayload(bucketKey, null, PostBucketsPayload.PolicyKeyEnum.Transient /* erase after 24h*/);
            BucketsApi         bucketsApi = new BucketsApi();

            bucketsApi.Configuration.AccessToken = bearer.access_token;
            dynamic newBucket = bucketsApi.CreateBucket(postBucket);

            // upload file (a.k.a. Objects)
            ObjectsApi objectsApi = new ObjectsApi();

            oauthApi.Configuration.AccessToken = bearer.access_token;
            dynamic newObject;

            using (StreamReader fileStream = new StreamReader(fileSavePath))
            {
                newObject = objectsApi.UploadObject(bucketKey, fileName,
                                                    (int)fileStream.BaseStream.Length, fileStream.BaseStream,
                                                    "application/octet-stream");
            }

            // translate file
            string objectIdBase64 = ToBase64(newObject.objectId);
            List <JobPayloadItem> postTranslationOutput = new List <JobPayloadItem>()
            {
                new JobPayloadItem(
                    JobPayloadItem.TypeEnum.Svf /* Viewer*/,
                    new List <JobPayloadItem.ViewsEnum>()
                {
                    JobPayloadItem.ViewsEnum._3d,
                    JobPayloadItem.ViewsEnum._2d
                })
            };

            JobPayload postTranslation = new JobPayload(
                new JobPayloadInput(objectIdBase64),
                new JobPayloadOutput(postTranslationOutput));
            DerivativesApi derivativeApi = new DerivativesApi();

            derivativeApi.Configuration.AccessToken = bearer.access_token;
            dynamic translation = derivativeApi.Translate(postTranslation);

            // check if is ready
            int progress = 0;

            do
            {
                System.Threading.Thread.Sleep(1000); // wait 1 second
                try
                {
                    dynamic manifest = derivativeApi.GetManifest(objectIdBase64);
                    progress = (string.IsNullOrWhiteSpace(Regex.Match(manifest.progress, @"\d+").Value) ? 100 : Int32.Parse(Regex.Match(manifest.progress, @"\d+").Value));
                }
                catch (Exception ex)
                {
                }
            } while (progress < 100);

            // clean up
            System.IO.File.Delete(fileSavePath);
            //Directory.Delete(fileSavePath, true);

            return(RedirectToAction("DisplayModel", "Home", new { characterName = objectIdBase64 }));
        }
Пример #4
0
        public async Task CloneBucket()
        {
            var source = await _bucketsApi.CreateBucket(GenerateName("robot sensor"), RetentionRule(), _organization);

            var properties = new Dictionary <string, string> {
                { "color", "green" }, { "location", "west" }
            };

            var label = await Client.GetLabelsApi()
                        .CreateLabel(GenerateName("Cool Resource"), properties, _organization.Id);

            await _bucketsApi.AddLabel(label, source);

            var name = GenerateName("cloned");

            var cloned = await _bucketsApi.CloneBucket(name, source);

            Assert.AreEqual(name, cloned.Name);
            Assert.AreEqual(_organization.Id, cloned.OrgID);
            Assert.IsNull(cloned.Rp);
            Assert.AreEqual(1, cloned.RetentionRules.Count);
            Assert.AreEqual(3600, cloned.RetentionRules[0].EverySeconds);
            Assert.AreEqual(BucketRetentionRules.TypeEnum.Expire, cloned.RetentionRules[0].Type);

            var labels = await _bucketsApi.GetLabels(cloned);

            Assert.AreEqual(1, labels.Count);
            Assert.AreEqual(label.Id, labels[0].Id);
        }