Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MediaFile and url #103

Open
StewartAM opened this issue Sep 20, 2019 · 4 comments
Open

MediaFile and url #103

StewartAM opened this issue Sep 20, 2019 · 4 comments

Comments

@StewartAM
Copy link

Can I use a URL to a video as the string in the MediaFile constructor like this?
var inputFile = new MediaFile("https://myblobstore..blob.core.windows.net/videos/myVideo.mp4");

@APX403
Copy link

APX403 commented Aug 20, 2021

I would like to achive the same thing, any clues ?

@captainkidd5
Copy link

Any update on this?

@enagaraj89
Copy link

I am looking for the similar implementation by passing presigned url of an object stored on S3. Any update on this?

@nemtajo
Copy link

nemtajo commented Apr 13, 2022

MediaFile would have to internally download the mp4 file in order to process it with FFMpeg executable..
Why don't you write a separate download method and use MediaFile constructor afterwards?

Here is some code to help you download a file from S3:


        private void InitializeAmazonClients()
        {
            _credentialsValid = TryGetAwsCredentials(out _awsCredentials);
            if (!_credentialsValid)
                throw new Exception("Not able to find aws credentials.");
            if (_transcribeClient == null)
                _transcribeClient = new AmazonTranscribeServiceClient(_awsCredentials, Amazon.RegionEndpoint.EUWest2);
            if (_s3Client == null)
                _s3Client = new AmazonS3Client(_awsCredentials, Amazon.RegionEndpoint.EUWest2);
            if (_translateClient == null)
                _translateClient = new AmazonTranslateClient(_awsCredentials, Amazon.RegionEndpoint.EUWest1);
            if (_pollyClient == null)
                _pollyClient = new AmazonPollyClient(_awsCredentials, Amazon.RegionEndpoint.EUWest1);
        }

        private bool TryGetAwsCredentials(out AWSCredentials awsCredentials)
        {
            CreateSettingsStoreFolderIfNotExists();
            var chain = new CredentialProfileStoreChain();
            if (!chain.TryGetAWSCredentials(_settings.CredentialProfile, out awsCredentials))
            {
                CreateAwsCredentials();
                if (!chain.TryGetAWSCredentials(_settings.CredentialProfile, out awsCredentials))
                {
                    return false;
                }
            }
            return true;
        }

        private void CreateAwsCredentials()
        {
            var options = new CredentialProfileOptions
            {
                AccessKey = _settings.AccessKey,
                SecretKey = _settings.SecretKey
            };
            var profile = new CredentialProfile(_settings.CredentialProfile, options);
            profile.Region = RegionEndpoint.USWest1;
            var sharedFile = new SharedCredentialsFile();
            sharedFile.RegisterProfile(profile);
        }

        private static void CreateSettingsStoreFolderIfNotExists()
        {
            string settingsStoreFolder = "";
            #if BCL
            settingsStoreFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "AWSToolkit");
            #else
            settingsStoreFolder = Environment.GetEnvironmentVariable("HOME");
            if (string.IsNullOrEmpty(settingsStoreFolder))
            settingsStoreFolder = Environment.GetEnvironmentVariable("USERPROFILE");
            settingsStoreFolder = Path.Combine(settingsStoreFolder, "AppData", "Local", "AWSToolkit");
            #endif
            if (!Directory.Exists(settingsStoreFolder))
                Directory.CreateDirectory(settingsStoreFolder);
        }

        public bool DownloadFileFromS3(string filePath, string bucketName, string objectName)
        {
            try
            {
                var fileTransferUtility = new TransferUtility(_s3Client);
                fileTransferUtility.DownloadAsync(filePath, bucketName, objectName).Wait();
                return true;
            }
            catch (AmazonS3Exception e)
            {
                _logger.LogError($"Error encountered on server. Message:'{0}' when writing an object", e.Message);
            }
            catch (Exception e)
            {
                _logger.LogError($"Unknown encountered on server. Message:'{0}' when writing an object", e.Message);
            }
            return false;
        }

       
        public string UploadFileToS3(string filePath, string bucketName, string objectName = null)
        {
            try
            {
                var fileTransferUtility = new TransferUtility(_s3Client);
                string s3ObjectName = objectName ?? Path.GetFileName(filePath);
                var fileTransferUtilityRequest = new TransferUtilityUploadRequest
                {
                    BucketName = bucketName,
                    FilePath = filePath,
                    StorageClass = S3StorageClass.StandardInfrequentAccess,
                    PartSize = 6291456, // 6 MB.
                    Key = s3ObjectName,
                    CannedACL = S3CannedACL.PublicRead
                };
                fileTransferUtility.UploadAsync(fileTransferUtilityRequest).Wait();
                return s3ObjectName;
            }
            catch (AmazonS3Exception e)
            {
                _logger.LogError($"Error encountered on server. Message:'{0}' when writing an object", e.Message);
            }
            catch (Exception e)
            {
                _logger.LogError($"Unknown encountered on server. Message:'{0}' when writing an object", e.Message);
            }
            return null;
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants