|
29 | 29 | use tool_objectfs\local\store\object_client_base;
|
30 | 30 | use tool_objectfs\local\store\signed_url;
|
31 | 31 | use local_aws\admin_settings_aws_region;
|
| 32 | +use stdClass; |
32 | 33 | use Throwable;
|
33 | 34 |
|
34 | 35 | define('AWS_API_VERSION', '2006-03-01');
|
@@ -391,42 +392,6 @@ public function test_permissions($testdelete) {
|
391 | 392 | return $permissions;
|
392 | 393 | }
|
393 | 394 |
|
394 |
| - public function test_set_object_tag() { |
395 |
| - try { |
396 |
| - // First ensure a test object exists to put tags on. |
397 |
| - // Note this will override the existing object if exists. |
398 |
| - $key = $this->bucketkeyprefix . 'tagging_check_file'; |
399 |
| - |
400 |
| - $this->client->putObject([ |
401 |
| - 'Bucket' => $this->bucket, |
402 |
| - 'Key' => $key, |
403 |
| - 'Body' => 'test content' |
404 |
| - ]); |
405 |
| - |
406 |
| - // Next try to tag it - this will throw an exception if cannot set |
407 |
| - // (for example, because it does not have permissions to). |
408 |
| - $this->client->putObjectTagging([ |
409 |
| - 'Bucket' => $this->bucket, |
410 |
| - 'Key' => $key, |
411 |
| - 'Tagging' => [ |
412 |
| - 'TagSet' => [ |
413 |
| - [ |
414 |
| - 'Key' => 'test', |
415 |
| - 'Value' => 'test', |
416 |
| - ] |
417 |
| - ] |
418 |
| - ] |
419 |
| - ]); |
420 |
| - } catch (Throwable $e) { |
421 |
| - return (object) [ |
422 |
| - 'success' => false, |
423 |
| - 'details' => $e->getMessage(), |
424 |
| - ]; |
425 |
| - } |
426 |
| - |
427 |
| - // Success - no exceptions thrown. |
428 |
| - return (object) ['success' => true, 'details' => '']; |
429 |
| - } |
430 | 395 |
|
431 | 396 | /**
|
432 | 397 | * get_exception_details
|
@@ -905,30 +870,80 @@ public function test_range_request($filesystem) {
|
905 | 870 | return (object)['result' => false, 'error' => get_string('fixturefilemissing', 'tool_objectfs')];
|
906 | 871 | }
|
907 | 872 |
|
908 |
| - public function set_object_tags(string $contenthash, array $tags) { |
909 |
| - // First convert PHP array tags to XML string. |
910 |
| - $s3tags = []; |
911 |
| - |
912 |
| - foreach($tags as $key => $value) { |
913 |
| - $s3tags[] = [ |
914 |
| - 'Key' => $key, |
915 |
| - 'Value' => $value, |
916 |
| - ]; |
917 |
| - } |
918 |
| - |
919 |
| - $key = $this->bucketkeyprefix . $this->get_filepath_from_hash($contenthash); |
920 |
| - |
921 |
| - // Then put onto object. |
922 |
| - $this->client->putObjectTagging([ |
923 |
| - 'Bucket' => $this->bucket, |
924 |
| - 'Key' => $key, |
925 |
| - 'Tagging' => [ |
926 |
| - 'TagSet' => $s3tags |
927 |
| - ] |
928 |
| - ]); |
929 |
| - } |
930 |
| - |
931 |
| - public function supports_object_tagging(): bool { |
932 |
| - return true; |
933 |
| - } |
| 873 | + /** |
| 874 | + * Tests setting an objects tag. |
| 875 | + * @return stdClass containing 'success' and 'details' properties |
| 876 | + */ |
| 877 | + public function test_set_object_tag(): stdClass { |
| 878 | + try { |
| 879 | + // First ensure a test object exists to put tags on. |
| 880 | + // Note this will override the existing object if exists. |
| 881 | + $key = $this->bucketkeyprefix . 'tagging_check_file'; |
| 882 | + |
| 883 | + $this->client->putObject([ |
| 884 | + 'Bucket' => $this->bucket, |
| 885 | + 'Key' => $key, |
| 886 | + 'Body' => 'test content' |
| 887 | + ]); |
| 888 | + |
| 889 | + // Next try to tag it - this will throw an exception if cannot set |
| 890 | + // (for example, because it does not have permissions to). |
| 891 | + $this->client->putObjectTagging([ |
| 892 | + 'Bucket' => $this->bucket, |
| 893 | + 'Key' => $key, |
| 894 | + 'Tagging' => [ |
| 895 | + 'TagSet' => [ |
| 896 | + [ |
| 897 | + 'Key' => 'test', |
| 898 | + 'Value' => 'test', |
| 899 | + ] |
| 900 | + ] |
| 901 | + ] |
| 902 | + ]); |
| 903 | + } catch (Throwable $e) { |
| 904 | + return (object) [ |
| 905 | + 'success' => false, |
| 906 | + 'details' => $e->getMessage(), |
| 907 | + ]; |
| 908 | + } |
| 909 | + |
| 910 | + // Success - no exceptions thrown. |
| 911 | + return (object) ['success' => true, 'details' => '']; |
| 912 | + } |
| 913 | + |
| 914 | + /** |
| 915 | + * Set the given objects tags in the external store. |
| 916 | + * @param string $contenthash file content hash |
| 917 | + * @param array $tags array of key=>value pairs to set as tags. |
| 918 | + */ |
| 919 | + public function set_object_tags(string $contenthash, array $tags) { |
| 920 | + // First convert PHP array tags to XML string. |
| 921 | + $s3tags = []; |
| 922 | + |
| 923 | + foreach($tags as $key => $value) { |
| 924 | + $s3tags[] = [ |
| 925 | + 'Key' => $key, |
| 926 | + 'Value' => $value, |
| 927 | + ]; |
| 928 | + } |
| 929 | + |
| 930 | + $key = $this->bucketkeyprefix . $this->get_filepath_from_hash($contenthash); |
| 931 | + |
| 932 | + // Then put onto object. |
| 933 | + $this->client->putObjectTagging([ |
| 934 | + 'Bucket' => $this->bucket, |
| 935 | + 'Key' => $key, |
| 936 | + 'Tagging' => [ |
| 937 | + 'TagSet' => $s3tags |
| 938 | + ] |
| 939 | + ]); |
| 940 | + } |
| 941 | + |
| 942 | + /** |
| 943 | + * If the client supports object tagging feature. |
| 944 | + * @return bool true if supports, else false |
| 945 | + */ |
| 946 | + public function supports_object_tagging(): bool { |
| 947 | + return true; |
| 948 | + } |
934 | 949 | }
|
0 commit comments