|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sausin\LaravelOvh\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
|
6
|
|
|
|
|
7
|
|
|
class CreateTempUrlKey extends Command |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* The name and signature of the console command. |
|
11
|
|
|
* |
|
12
|
|
|
* @var string |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $signature = 'ovh:create-temp-url-key'; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* The console command description. |
|
18
|
|
|
* |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $description = 'Generate the temp url key, allowing the use of Storage::temporaryUrl()'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Create a new command instance. |
|
25
|
|
|
* |
|
26
|
|
|
* @return void |
|
|
|
|
|
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct() |
|
29
|
|
|
{ |
|
30
|
|
|
parent::__construct(); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Execute the console command. |
|
35
|
|
|
* |
|
36
|
|
|
* @return mixed |
|
37
|
|
|
*/ |
|
38
|
|
|
public function handle() |
|
39
|
|
|
{ |
|
40
|
|
|
// generate the key with sha512sum of time() |
|
41
|
|
|
|
|
42
|
|
|
$this->info("read https://docs.ovh.com/gb/en/public-cloud/share_an_object_via_a_temporary_url/#generate-the-key for explanation of what we're doing here"); |
|
43
|
|
|
|
|
44
|
|
|
$temp_url_key = hash('sha512', time()); |
|
45
|
|
|
$client = new \GuzzleHttp\Client(); |
|
46
|
|
|
|
|
47
|
|
|
// ( https://docs.ovh.com/gb/en/public-cloud/managing_tokens/ ) |
|
48
|
|
|
// Request token creation |
|
49
|
|
|
// curl -X POST ${OS_AUTH_URL}auth/tokens -H "Content-Type: application/json" -d ' { "auth": { "identity": { "methods": ["password"], "password": { "user": { "name": "'$OS_USERNAME'", "domain": { "id": "default" }, "password": "'$OS_PASSWORD'" } } }, "scope": { "project": { "name": "'$OS_TENANT_NAME'", "domain": { "id": "default" } } } } }' | python -mjson.tool |
|
50
|
|
|
|
|
51
|
|
|
$payload = [ |
|
52
|
|
|
"auth" => [ |
|
53
|
|
|
"identity" => [ |
|
54
|
|
|
"methods" => [ |
|
55
|
|
|
"password" |
|
56
|
|
|
], |
|
57
|
|
|
"password" => [ |
|
58
|
|
|
"user" => [ |
|
59
|
|
|
"name" => config('filesystems.disks.ovh.user'), |
|
60
|
|
|
"domain" => [ |
|
61
|
|
|
"id" => "default" |
|
62
|
|
|
], |
|
63
|
|
|
"password" => config('filesystems.disks.ovh.pass') |
|
64
|
|
|
] |
|
65
|
|
|
] |
|
66
|
|
|
], |
|
67
|
|
|
"scope" => [ |
|
68
|
|
|
"project" => [ |
|
69
|
|
|
"name" => config('filesystems.disks.ovh.tenantName'), |
|
70
|
|
|
"domain" => [ |
|
71
|
|
|
"id" => "default" |
|
72
|
|
|
] |
|
73
|
|
|
] |
|
74
|
|
|
] |
|
75
|
|
|
], |
|
76
|
|
|
]; |
|
77
|
|
|
|
|
78
|
|
|
$this->info("getting auth token from ".config('filesystems.disks.ovh.server').'auth/tokens'); |
|
79
|
|
|
|
|
80
|
|
|
$response = $client->request('POST', config('filesystems.disks.ovh.server').'auth/tokens', [ |
|
81
|
|
|
'json' => $payload |
|
82
|
|
|
]); |
|
83
|
|
|
|
|
84
|
|
|
$data = json_decode($response->getBody()); |
|
85
|
|
|
|
|
86
|
|
|
// Retrieve the token ID variables and publicURL endpoint |
|
87
|
|
|
foreach($data->token->catalog as $catalog): |
|
88
|
|
|
if ( $catalog->type == 'object-store' ): |
|
89
|
|
|
foreach($catalog->endpoints as $endpoint): |
|
90
|
|
|
if ( $endpoint->region == config('filesystems.disks.ovh.region') ): |
|
91
|
|
|
// $auth_token = $endpoint->id; |
|
92
|
|
|
$endpoint = $endpoint->url; |
|
93
|
|
|
break(2); |
|
94
|
|
|
endif; |
|
95
|
|
|
endforeach; |
|
96
|
|
|
endif; |
|
97
|
|
|
endforeach; |
|
98
|
|
|
|
|
99
|
|
|
// export token=$(curl -is -X POST ${OS_AUTH_URL}auth/tokens -H "Content-Type: application/json" -d ' { "auth": { "identity": { "methods": ["password"], "password": { "user": { "name": "'$OS_USERNAME'", "domain": { "id": "default" }, "password": "'$OS_PASSWORD'" } } }, "scope": { "project": { "name": "'$OS_TENANT_NAME'", "domain": { "id": "default" } } } } }' | grep '^X-Subject-Token' | cut -d" " -f2) |
|
100
|
|
|
$headers = $response->getHeaders(); |
|
101
|
|
|
$auth_token = $headers["X-Subject-Token"][0]; |
|
102
|
|
|
|
|
103
|
|
|
|
|
104
|
|
|
// then define the temp-url-key header ( https://docs.ovh.com/gb/en/public-cloud/share_an_object_via_a_temporary_url/#generate-the-key ) |
|
105
|
|
|
// curl -i -X POST \ -H "X-Account-Meta-Temp-URL-Key: 12345" \ -H "X-Auth-Token: abcdef12345" \ https://storage.sbg1.cloud.ovh.net/v1/AUTH_ProjectID |
|
106
|
|
|
|
|
107
|
|
|
$payload = [ |
|
108
|
|
|
'headers' => [ |
|
109
|
|
|
'X-Account-Meta-Temp-URL-Key' => $temp_url_key, |
|
110
|
|
|
'X-Auth-Token' => $auth_token |
|
111
|
|
|
] |
|
112
|
|
|
]; |
|
113
|
|
|
$this->info("posting to ".$endpoint); |
|
|
|
|
|
|
114
|
|
|
|
|
115
|
|
|
$response = $client->request('POST', $endpoint, $payload); |
|
|
|
|
|
|
116
|
|
|
|
|
117
|
|
|
$this->alert("add this line to your .env file"); |
|
118
|
|
|
$this->info("OVH_URL_KEY=".$temp_url_key.PHP_EOL); |
|
119
|
|
|
|
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
|
|
123
|
|
|
|
|
124
|
|
|
} |
|
125
|
|
|
|
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.