azure_media_service 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +68 -0
- data/Rakefile +11 -0
- data/azure_media_service.gemspec +29 -0
- data/lib/azure_media_service.rb +58 -0
- data/lib/azure_media_service/config.rb +10 -0
- data/lib/azure_media_service/errors.rb +2 -0
- data/lib/azure_media_service/model.rb +7 -0
- data/lib/azure_media_service/model/access_policy.rb +7 -0
- data/lib/azure_media_service/model/asset.rb +197 -0
- data/lib/azure_media_service/model/asset_file.rb +7 -0
- data/lib/azure_media_service/model/base.rb +16 -0
- data/lib/azure_media_service/model/job.rb +32 -0
- data/lib/azure_media_service/model/locator.rb +7 -0
- data/lib/azure_media_service/model/media_processor.rb +7 -0
- data/lib/azure_media_service/request.rb +140 -0
- data/lib/azure_media_service/service.rb +97 -0
- data/lib/azure_media_service/version.rb +3 -0
- data/spec/azure_media_service_ruby_spec.rb +5 -0
- data/spec/spec_helper.rb +4 -0
- metadata +166 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 08fdee82fc0f65b5448bafb95f064da9ed031492
|
4
|
+
data.tar.gz: 47143a23d14af482ba7232029c680c69aa0ad629
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8a4528a7cebb310197623e84d367860596e42990b621d06a11935f8d0e9c0233e46ae19b832033a4cfcaf29749f08cd5b6bb331a9a460b1f4a2517c82c0b6f92
|
7
|
+
data.tar.gz: 5f65881823d38b3db53843a04e2194227d830c7bdfff79492cbac328453bf6c6831e96e07314d7588ba2cba1f5534544128e9eff35dc8ba0030c65599a54da24
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 murayama
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
# AzureMediaServiceRuby
|
2
|
+
|
3
|
+
Windows Azure Media Service API Client
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'azure_media_service'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install azure_media_service
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
|
23
|
+
# initialize
|
24
|
+
AzureMediaService.configure do |config|
|
25
|
+
cofig.id = 'xxxxxxxx'
|
26
|
+
config.secret = 'xxxxxxxxxxxxxxxxxx'
|
27
|
+
end
|
28
|
+
|
29
|
+
# service instance
|
30
|
+
ams = AzureMediaService.service
|
31
|
+
|
32
|
+
# upload file
|
33
|
+
asset = ams.upload_media('path/to/example.mp4')
|
34
|
+
|
35
|
+
# encode job
|
36
|
+
asset = ams.assets('nb:cid:UUID:xxxxxxxxxxx-xxxxxxxxxxx-xxxxxx-xxxxxxx')
|
37
|
+
job = asset.encode_job('H264 Smooth Streaming 720p')
|
38
|
+
|
39
|
+
# or
|
40
|
+
job = ams.create_encode_job('nb:cid:UUID:xxxxxxxxxxx-xxxxxxxxxxx-xxxxxx-xxxxxxx', 'H264 Smooth Streaming 720p')
|
41
|
+
|
42
|
+
# publish asset
|
43
|
+
asset = ams.assets('nb:cid:UUID:xxxxxxxxxxx-xxxxxxxxxxx-xxxxxx-xxxxxxx')
|
44
|
+
asset.publish(expire_minutes: 43200)
|
45
|
+
|
46
|
+
# or
|
47
|
+
ams.publish('nb:cid:UUID:xxxxxxxxxxx-xxxxxxxxxxx-xxxxxx-xxxxxxx', 43200)
|
48
|
+
```
|
49
|
+
|
50
|
+
### Custom Encode Task
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
AzureMediaService.configure do |config|
|
54
|
+
config.add_encode_task('Custom Encode Task', File.read('path/to/custome_task.xml'))
|
55
|
+
end
|
56
|
+
|
57
|
+
ams = AzureMediaService.service
|
58
|
+
asset = ams.assets('nb:cid:UUID:xxxxxxxxxxx-xxxxxxxxxxx-xxxxxx-xxxxxxx')
|
59
|
+
asset.encode_job('Custom Encode Task')
|
60
|
+
```
|
61
|
+
|
62
|
+
## Contributing
|
63
|
+
|
64
|
+
1. Fork it ( https://github.com/murayama/azure_media_service_ruby/fork )
|
65
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
66
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
67
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
68
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'azure_media_service/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "azure_media_service"
|
8
|
+
spec.version = AzureMediaService::VERSION
|
9
|
+
spec.authors = ["murayama"]
|
10
|
+
spec.email = ["mura869@gmail.com"]
|
11
|
+
spec.summary = %q{Azure Media Service SDK for ruby}
|
12
|
+
spec.description = %q{Azure Media Service SDK for ruby}
|
13
|
+
spec.homepage = "https://github.com/murayama/azure_media_service_ruby"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
|
25
|
+
spec.add_dependency "faraday"
|
26
|
+
spec.add_dependency "faraday_middleware"
|
27
|
+
spec.add_dependency "mime-types"
|
28
|
+
spec.add_dependency "builder"
|
29
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'azure_media_service/version'
|
2
|
+
require 'azure_media_service/errors'
|
3
|
+
require 'azure_media_service/config'
|
4
|
+
require 'azure_media_service/request'
|
5
|
+
require 'azure_media_service/service'
|
6
|
+
require 'azure_media_service/model'
|
7
|
+
|
8
|
+
require 'faraday'
|
9
|
+
require 'faraday_middleware'
|
10
|
+
require 'time'
|
11
|
+
require 'mime/types'
|
12
|
+
require 'base64'
|
13
|
+
require 'builder/xmlmarkup'
|
14
|
+
|
15
|
+
module AzureMediaService
|
16
|
+
|
17
|
+
@@tasks = {}
|
18
|
+
|
19
|
+
class << self
|
20
|
+
|
21
|
+
def configure
|
22
|
+
yield self
|
23
|
+
end
|
24
|
+
|
25
|
+
def request
|
26
|
+
@@request ||= Request.new(client_id:@@id, client_secret:@@secret)
|
27
|
+
end
|
28
|
+
|
29
|
+
def service
|
30
|
+
@@service ||= Service.new
|
31
|
+
end
|
32
|
+
|
33
|
+
def id=(v)
|
34
|
+
@@id = v
|
35
|
+
end
|
36
|
+
|
37
|
+
def id
|
38
|
+
@@id
|
39
|
+
end
|
40
|
+
|
41
|
+
def secret=(v)
|
42
|
+
@@secret = v
|
43
|
+
end
|
44
|
+
|
45
|
+
def secret
|
46
|
+
@@secret
|
47
|
+
end
|
48
|
+
|
49
|
+
def add_encode_task(name, task)
|
50
|
+
@@tasks[name] = task
|
51
|
+
end
|
52
|
+
|
53
|
+
def encode_tasks
|
54
|
+
@@tasks ||= {}
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,7 @@
|
|
1
|
+
require 'azure_media_service/model/base'
|
2
|
+
require 'azure_media_service/model/asset'
|
3
|
+
require 'azure_media_service/model/access_policy'
|
4
|
+
require 'azure_media_service/model/locator'
|
5
|
+
require 'azure_media_service/model/asset_file'
|
6
|
+
require 'azure_media_service/model/media_processor'
|
7
|
+
require 'azure_media_service/model/job'
|
@@ -0,0 +1,197 @@
|
|
1
|
+
module AzureMediaService
|
2
|
+
module Model
|
3
|
+
class Asset < Base
|
4
|
+
|
5
|
+
def locators
|
6
|
+
@locators ||= []
|
7
|
+
if @locators.empty?
|
8
|
+
_uri = URI.parse(self.Locators["__deferred"]["uri"])
|
9
|
+
url = _uri.path.gsub('/api/','')
|
10
|
+
res = @request.get(url)
|
11
|
+
res["d"]["results"].each do |v|
|
12
|
+
@locators << Model::Locator.new(v)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
@locators
|
16
|
+
end
|
17
|
+
|
18
|
+
def files
|
19
|
+
@files ||= []
|
20
|
+
if @files.empty?
|
21
|
+
_uri = URI.parse(self.Files["__deferred"]["uri"])
|
22
|
+
url = _uri.path.gsub('/api/','')
|
23
|
+
res = @request.get(url)
|
24
|
+
res["d"]["results"].each do |v|
|
25
|
+
@files << Model::AssetFile.new(v)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
@files
|
29
|
+
end
|
30
|
+
|
31
|
+
def upload(filepath)
|
32
|
+
begin
|
33
|
+
mime_type = MIME::Types.type_for(filepath)[0].to_s
|
34
|
+
basename = File.basename(filepath, '.*')
|
35
|
+
filename = File.basename(filepath)
|
36
|
+
f = Faraday::UploadIO.new(filepath, mime_type)
|
37
|
+
|
38
|
+
# create policy
|
39
|
+
policy = @service.create_access_policy(name:"UploadPolicy", duration_minutes:1800, permission:2)
|
40
|
+
|
41
|
+
# create Locator
|
42
|
+
locator = @service.create_locator(policy_id:policy.Id, asset_id:self.Id, type:1)
|
43
|
+
|
44
|
+
# upload
|
45
|
+
upload_url = File.join(locator.BaseUri, filename)
|
46
|
+
upload_url += locator.ContentAccessComponent
|
47
|
+
|
48
|
+
if f.size > Config::UPLOAD_LIMIT_SIZE
|
49
|
+
# put block and put block list API
|
50
|
+
i = 1
|
51
|
+
blockids = []
|
52
|
+
while buf = f.read(Config::READ_BUFFER_SIZE) do
|
53
|
+
id = "%05d" % i # サイズを同じにしなければいけないので、5桁にする
|
54
|
+
blockid = Base64.strict_encode64("#{id}-#{filename}")
|
55
|
+
blockids << blockid
|
56
|
+
put_block_url = upload_url + "&comp=block&blockid=#{URI.escape(blockid)}"
|
57
|
+
res = put_blob(url:put_block_url, blob:buf)
|
58
|
+
i+=1
|
59
|
+
end
|
60
|
+
|
61
|
+
put_block_list_url = upload_url + "&comp=blocklist"
|
62
|
+
let = ''
|
63
|
+
xml = Builder::XmlMarkup.new(:target => let, :indent => 3)
|
64
|
+
xml.instruct!
|
65
|
+
xml.BlockList {
|
66
|
+
blockids.each do |id|
|
67
|
+
xml.Latest id
|
68
|
+
end
|
69
|
+
}
|
70
|
+
|
71
|
+
res = @request.put(put_block_list_url, let) do |headers|
|
72
|
+
headers['x-ms-date'] = Time.now.httpdate
|
73
|
+
headers['x-ms-version'] = '2014-02-14'
|
74
|
+
headers['Content-Type'] = 'text/plain; charset = UTF-8'
|
75
|
+
headers['Content-Length'] = let.size.to_s
|
76
|
+
end
|
77
|
+
|
78
|
+
else
|
79
|
+
res = put_blob(url:upload_url, blob:f)
|
80
|
+
end
|
81
|
+
|
82
|
+
# create metadata
|
83
|
+
@request.get("CreateFileInfos", {"assetid" => "'#{URI.encode(self.Id)}'"})
|
84
|
+
|
85
|
+
clear_cache
|
86
|
+
rescue => e
|
87
|
+
raise MediaServiceError.new(e.message)
|
88
|
+
end
|
89
|
+
self
|
90
|
+
end
|
91
|
+
|
92
|
+
|
93
|
+
def encode_job(encode_configuration)
|
94
|
+
media_processor = @service.media_processor_id_by_name('Windows Azure Media Encoder')
|
95
|
+
|
96
|
+
conf_str = encode_configuration.gsub(' ', '_')
|
97
|
+
|
98
|
+
if AzureMediaService.encode_tasks.has_key?(encode_configuration)
|
99
|
+
encode_configuration = AzureMediaService.encode_tasks[encode_configuration]
|
100
|
+
end
|
101
|
+
|
102
|
+
job_name, output_name = job_and_output_name(asset_name:self.Name, conf:conf_str)
|
103
|
+
|
104
|
+
post_body = {}
|
105
|
+
post_body["Name"] = job_name
|
106
|
+
post_body["InputMediaAssets"] = [
|
107
|
+
{
|
108
|
+
"__metadata" => {
|
109
|
+
"uri" => self.__metadata["uri"]
|
110
|
+
}
|
111
|
+
}
|
112
|
+
]
|
113
|
+
task_body = ''
|
114
|
+
xml = Builder::XmlMarkup.new(:target => task_body)
|
115
|
+
xml.instruct!
|
116
|
+
xml.taskBody {
|
117
|
+
xml.inputAsset 'JobInputAsset(0)'
|
118
|
+
xml.outputAsset 'JobOutputAsset(0)', :assetName => output_name
|
119
|
+
}
|
120
|
+
post_body["Tasks"] = [
|
121
|
+
{
|
122
|
+
"Configuration" => encode_configuration,
|
123
|
+
"MediaProcessorId" => media_processor.Id,
|
124
|
+
"TaskBody" => task_body
|
125
|
+
}
|
126
|
+
]
|
127
|
+
|
128
|
+
res = @request.post('Jobs', post_body)
|
129
|
+
Model::Job.new(res["d"])
|
130
|
+
end
|
131
|
+
|
132
|
+
|
133
|
+
def publish(expire_minutes: 43200)
|
134
|
+
locator = locators.select {|l| l.Type == 2}.first
|
135
|
+
unless locator
|
136
|
+
policy = @service.create_access_policy(name:"PublishPolicy", duration_minutes: expire_minutes, permission:1)
|
137
|
+
locator = @service.create_locator(policy_id: policy.Id, asset_id: self.Id, type: 2)
|
138
|
+
end
|
139
|
+
locator
|
140
|
+
end
|
141
|
+
|
142
|
+
def publish_url
|
143
|
+
primary_file = files.select {|f| f.IsPrimary == true}.first
|
144
|
+
locator = locators.select {|l| l.Type == 2 }.first
|
145
|
+
if primary_file && locator
|
146
|
+
File.join(locator.Path, primary_file.Name, 'Manifest')
|
147
|
+
else
|
148
|
+
nil
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
def delete
|
153
|
+
begin
|
154
|
+
res = @request.delete("Assets('#{self.Id}')")
|
155
|
+
clear_cache
|
156
|
+
rescue => e
|
157
|
+
raise MediaServiceError.new(e.message)
|
158
|
+
end
|
159
|
+
res
|
160
|
+
end
|
161
|
+
|
162
|
+
private
|
163
|
+
|
164
|
+
def clear_cache
|
165
|
+
@locators = nil
|
166
|
+
@files = nil
|
167
|
+
self
|
168
|
+
end
|
169
|
+
|
170
|
+
def job_and_output_name(asset_name:, conf:)
|
171
|
+
job_names = []
|
172
|
+
job_names << asset_name
|
173
|
+
job_names << 'EncodeJob'
|
174
|
+
job_names << conf
|
175
|
+
job_name = job_names.join('-')
|
176
|
+
|
177
|
+
output_names = []
|
178
|
+
output_names << asset_name
|
179
|
+
output_names << conf
|
180
|
+
output_names << 'Output'
|
181
|
+
output_name = output_names.join('-')
|
182
|
+
|
183
|
+
[job_name, output_name]
|
184
|
+
end
|
185
|
+
|
186
|
+
def put_blob(url:, blob:)
|
187
|
+
res = @request.put(url, blob) do |headers|
|
188
|
+
headers['x-ms-blob-type'] = 'BlockBlob'
|
189
|
+
headers['x-ms-version'] = '2014-02-14' # Storage API Version
|
190
|
+
headers['Content-Type'] = 'application/octet-stream'
|
191
|
+
headers['Content-Length'] = blob.size.to_s
|
192
|
+
end
|
193
|
+
end
|
194
|
+
end
|
195
|
+
|
196
|
+
end
|
197
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
module AzureMediaService
|
3
|
+
module Model
|
4
|
+
class Base < OpenStruct
|
5
|
+
|
6
|
+
attr_reader :original_data
|
7
|
+
|
8
|
+
def initialize(hash)
|
9
|
+
super
|
10
|
+
@original_data = hash
|
11
|
+
@request = AzureMediaService.request
|
12
|
+
@service = AzureMediaService.service
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module AzureMediaService
|
2
|
+
module Model
|
3
|
+
class Job < Base
|
4
|
+
|
5
|
+
def output_assets
|
6
|
+
@output_assets ||= []
|
7
|
+
if @output_assets.empty?
|
8
|
+
_uri = URI.parse(self.OutputMediaAssets["__deferred"]["uri"])
|
9
|
+
url = _uri.path.gsub('/api/','')
|
10
|
+
res = @request.get(url)
|
11
|
+
res["d"]["results"].each do |v|
|
12
|
+
@output_assets << Model::Asset.new(v)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
@output_assets
|
16
|
+
end
|
17
|
+
|
18
|
+
def input_assets
|
19
|
+
@input_assets ||= []
|
20
|
+
if @input_assets.empty?
|
21
|
+
_uri = URI.parse(self.InputMediaAssets["__deferred"]["uri"])
|
22
|
+
url = _uri.path.gsub('/api/','')
|
23
|
+
res = @request.get(url)
|
24
|
+
res["d"]["results"].each do |v|
|
25
|
+
@input_assets << Model::Asset.new(v)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
@input_assets
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,140 @@
|
|
1
|
+
module AzureMediaService
|
2
|
+
class Request
|
3
|
+
|
4
|
+
def initialize(config)
|
5
|
+
build_config(config)
|
6
|
+
end
|
7
|
+
|
8
|
+
def get(endpoint, params={})
|
9
|
+
|
10
|
+
setToken() if token_expire?
|
11
|
+
|
12
|
+
res = conn(@config[:mediaURI]).get do |req|
|
13
|
+
req.url URI.escape(endpoint, '():')
|
14
|
+
req.headers = @default_headers
|
15
|
+
req.headers[:Authorization] = "Bearer #{@access_token}"
|
16
|
+
req.params = params
|
17
|
+
end
|
18
|
+
|
19
|
+
if res.status == 301
|
20
|
+
@config[:mediaURI] = res.headers['location']
|
21
|
+
get(endpoint, params)
|
22
|
+
else
|
23
|
+
res.body
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def post(endpoint, body)
|
28
|
+
setToken if token_expire?
|
29
|
+
|
30
|
+
res = conn(@config[:mediaURI]).post do |req|
|
31
|
+
req.url endpoint
|
32
|
+
req.headers = @default_headers
|
33
|
+
req.headers[:Authorization] = "Bearer #{@access_token}"
|
34
|
+
req.body = body
|
35
|
+
end
|
36
|
+
|
37
|
+
if res.status == 301
|
38
|
+
@config[:mediaURI] = res.headers['location']
|
39
|
+
post(endpoint, body)
|
40
|
+
else
|
41
|
+
res.body
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def put(url, body)
|
46
|
+
|
47
|
+
_conn = conn(url) do |builder|
|
48
|
+
builder.request :multipart
|
49
|
+
end
|
50
|
+
|
51
|
+
headers = {}
|
52
|
+
|
53
|
+
if block_given?
|
54
|
+
yield(headers)
|
55
|
+
end
|
56
|
+
|
57
|
+
res = _conn.put do |req|
|
58
|
+
req.headers = headers
|
59
|
+
req.body = body
|
60
|
+
end
|
61
|
+
if res.status == 301
|
62
|
+
@config[:mediaURI] = res.headers['location']
|
63
|
+
put(url, body)
|
64
|
+
else
|
65
|
+
res.body
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def delete(endpoint, params={})
|
70
|
+
|
71
|
+
setToken() if token_expire?
|
72
|
+
|
73
|
+
res = conn(@config[:mediaURI]).delete do |req|
|
74
|
+
req.url URI.escape(endpoint, '():')
|
75
|
+
req.headers = @default_headers
|
76
|
+
req.headers[:Authorization] = "Bearer #{@access_token}"
|
77
|
+
req.params = params
|
78
|
+
end
|
79
|
+
|
80
|
+
if res.status == 301
|
81
|
+
@config[:mediaURI] = res.headers['location']
|
82
|
+
delete(endpoint, params)
|
83
|
+
else
|
84
|
+
res.body
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
private
|
89
|
+
def build_config(config)
|
90
|
+
|
91
|
+
@config = config || {}
|
92
|
+
# @config[:mediaURI] = "https://media.windows.net/API/"
|
93
|
+
@config[:mediaURI] = Config::MEDIA_URI
|
94
|
+
@config[:tokenURI] = Config::TOKEN_URI
|
95
|
+
@config[:client_id] ||= ''
|
96
|
+
@config[:client_secret] ||= ''
|
97
|
+
|
98
|
+
@default_headers = {
|
99
|
+
"Content-Type" => "application/json;odata=verbose",
|
100
|
+
"Accept" => "application/json;odata=verbose",
|
101
|
+
"DataServiceVersion" => "3.0",
|
102
|
+
"MaxDataServiceVersion" => "3.0",
|
103
|
+
"x-ms-version" => "2.5"
|
104
|
+
}
|
105
|
+
end
|
106
|
+
|
107
|
+
def conn(url)
|
108
|
+
conn = Faraday::Connection.new(:url => url, :ssl => {:verify => false}) do |builder|
|
109
|
+
builder.request :url_encoded
|
110
|
+
# builder.response :logger
|
111
|
+
builder.use FaradayMiddleware::EncodeJson
|
112
|
+
builder.use FaradayMiddleware::ParseJson, :content_type => /\bjson$/
|
113
|
+
builder.adapter Faraday.default_adapter
|
114
|
+
if block_given?
|
115
|
+
yield(builder)
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
def setToken
|
121
|
+
res = conn(@config[:tokenURI]).post do |req|
|
122
|
+
req.body = {
|
123
|
+
client_id: @config[:client_id],
|
124
|
+
client_secret: @config[:client_secret],
|
125
|
+
grant_type: 'client_credentials',
|
126
|
+
scope: 'urn:WindowsAzureMediaServices'
|
127
|
+
}
|
128
|
+
end
|
129
|
+
|
130
|
+
@access_token = res.body["access_token"]
|
131
|
+
@token_expires = Time.now.to_i + res.body["expires_in"].to_i
|
132
|
+
end
|
133
|
+
|
134
|
+
def token_expire?
|
135
|
+
return true unless @access_token
|
136
|
+
return true if Time.now.to_i >= @token_expires
|
137
|
+
return false
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
module AzureMediaService
|
2
|
+
class Service
|
3
|
+
|
4
|
+
def initialize
|
5
|
+
@request = AzureMediaService.request
|
6
|
+
end
|
7
|
+
|
8
|
+
# assets
|
9
|
+
def assets(asset_id=nil)
|
10
|
+
get('Assets', Model::Asset, asset_id)
|
11
|
+
end
|
12
|
+
|
13
|
+
# assets create
|
14
|
+
def create_asset(name)
|
15
|
+
post_body = {
|
16
|
+
"Name" => name
|
17
|
+
}
|
18
|
+
res = @request.post("Assets", post_body)
|
19
|
+
Model::Asset.new(res["d"])
|
20
|
+
end
|
21
|
+
|
22
|
+
# access policy create
|
23
|
+
def create_access_policy(name:'Policy', duration_minutes:300, permission:2)
|
24
|
+
post_body = {
|
25
|
+
"Name" => name,
|
26
|
+
"DurationInMinutes" => duration_minutes,
|
27
|
+
"Permissions" => permission
|
28
|
+
}
|
29
|
+
res = @request.post("AccessPolicies", post_body)
|
30
|
+
Model::AccessPolicy.new(res["d"])
|
31
|
+
end
|
32
|
+
|
33
|
+
# locator create
|
34
|
+
def create_locator(policy_id:,asset_id:,type:1)
|
35
|
+
post_body = {
|
36
|
+
"AccessPolicyId" => policy_id,
|
37
|
+
"AssetId" => asset_id,
|
38
|
+
"Type" => type,
|
39
|
+
"StartTime" => (Time.now - 5*60).gmtime.strftime('%Y-%m-%dT%H:%M:%SZ')
|
40
|
+
}
|
41
|
+
res = @request.post("Locators", post_body)
|
42
|
+
Model::Locator.new(res["d"])
|
43
|
+
end
|
44
|
+
|
45
|
+
def upload_media(filepath)
|
46
|
+
basename = File.basename(filepath, '.*')
|
47
|
+
asset_name = "#{basename}-Source-#{Time.now.strftime('%Y%m%d%H%M%S')}"
|
48
|
+
asset = create_asset(asset_name)
|
49
|
+
asset.upload(filepath)
|
50
|
+
end
|
51
|
+
|
52
|
+
def create_encode_job(asset_id, encode_configuration)
|
53
|
+
asset = assets(asset_id)
|
54
|
+
asset.encode_job(encode_configuration)
|
55
|
+
end
|
56
|
+
|
57
|
+
def jobs(job_id=nil)
|
58
|
+
get('Jobs', Model::Job, job_id)
|
59
|
+
end
|
60
|
+
|
61
|
+
# publish asset
|
62
|
+
def publish(asset_id, expire_minutes=nil)
|
63
|
+
asset = assets(asset_id)
|
64
|
+
asset.publish(expire_minutes)
|
65
|
+
end
|
66
|
+
|
67
|
+
def publish_url(asset_id)
|
68
|
+
asset = assets(asset_id)
|
69
|
+
asset.publish_url
|
70
|
+
end
|
71
|
+
|
72
|
+
def media_processor_id_by_name(name)
|
73
|
+
res = @request.get('MediaProcessors')
|
74
|
+
mp = res["d"]["results"].select {|v|
|
75
|
+
v["Name"] == 'Windows Azure Media Encoder'
|
76
|
+
}.sort{|a,b|
|
77
|
+
b["Version"].to_i <=> a["Version"].to_i
|
78
|
+
}.first
|
79
|
+
Model::MediaProcessor.new(mp)
|
80
|
+
end
|
81
|
+
|
82
|
+
def get(method, klass, id=nil)
|
83
|
+
if id.nil?
|
84
|
+
res = @request.get(method)
|
85
|
+
results = []
|
86
|
+
res["d"]["results"].each do |a|
|
87
|
+
results << klass.new(a)
|
88
|
+
end
|
89
|
+
else
|
90
|
+
res = @request.get("#{method}('#{id}')")
|
91
|
+
results = klass.new(res["d"])
|
92
|
+
end
|
93
|
+
results
|
94
|
+
end
|
95
|
+
|
96
|
+
end
|
97
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,166 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: azure_media_service
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- murayama
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-11-04 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: faraday
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: faraday_middleware
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: mime-types
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: builder
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :runtime
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
description: Azure Media Service SDK for ruby
|
112
|
+
email:
|
113
|
+
- mura869@gmail.com
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- ".gitignore"
|
119
|
+
- Gemfile
|
120
|
+
- LICENSE.txt
|
121
|
+
- README.md
|
122
|
+
- Rakefile
|
123
|
+
- azure_media_service.gemspec
|
124
|
+
- lib/azure_media_service.rb
|
125
|
+
- lib/azure_media_service/config.rb
|
126
|
+
- lib/azure_media_service/errors.rb
|
127
|
+
- lib/azure_media_service/model.rb
|
128
|
+
- lib/azure_media_service/model/access_policy.rb
|
129
|
+
- lib/azure_media_service/model/asset.rb
|
130
|
+
- lib/azure_media_service/model/asset_file.rb
|
131
|
+
- lib/azure_media_service/model/base.rb
|
132
|
+
- lib/azure_media_service/model/job.rb
|
133
|
+
- lib/azure_media_service/model/locator.rb
|
134
|
+
- lib/azure_media_service/model/media_processor.rb
|
135
|
+
- lib/azure_media_service/request.rb
|
136
|
+
- lib/azure_media_service/service.rb
|
137
|
+
- lib/azure_media_service/version.rb
|
138
|
+
- spec/azure_media_service_ruby_spec.rb
|
139
|
+
- spec/spec_helper.rb
|
140
|
+
homepage: https://github.com/murayama/azure_media_service_ruby
|
141
|
+
licenses:
|
142
|
+
- MIT
|
143
|
+
metadata: {}
|
144
|
+
post_install_message:
|
145
|
+
rdoc_options: []
|
146
|
+
require_paths:
|
147
|
+
- lib
|
148
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
153
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
154
|
+
requirements:
|
155
|
+
- - ">="
|
156
|
+
- !ruby/object:Gem::Version
|
157
|
+
version: '0'
|
158
|
+
requirements: []
|
159
|
+
rubyforge_project:
|
160
|
+
rubygems_version: 2.2.2
|
161
|
+
signing_key:
|
162
|
+
specification_version: 4
|
163
|
+
summary: Azure Media Service SDK for ruby
|
164
|
+
test_files:
|
165
|
+
- spec/azure_media_service_ruby_spec.rb
|
166
|
+
- spec/spec_helper.rb
|