1
0

S3Test.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Robin Appelman <robin@icewind.nl>
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace Test\Files\ObjectStore;
  22. use Icewind\Streams\Wrapper;
  23. use OC\Files\ObjectStore\S3;
  24. class MultiPartUploadS3 extends S3 {
  25. public function writeObject($urn, $stream, string $mimetype = null) {
  26. $this->getConnection()->upload($this->bucket, $urn, $stream, 'private', [
  27. 'mup_threshold' => 1,
  28. ]);
  29. }
  30. }
  31. class NonSeekableStream extends Wrapper {
  32. public static function wrap($source) {
  33. $context = stream_context_create([
  34. 'nonseek' => [
  35. 'source' => $source,
  36. ],
  37. ]);
  38. return Wrapper::wrapSource($source, $context, 'nonseek', self::class);
  39. }
  40. public function dir_opendir($path, $options) {
  41. return false;
  42. }
  43. public function stream_open($path, $mode, $options, &$opened_path) {
  44. $this->loadContext('nonseek');
  45. return true;
  46. }
  47. public function stream_seek($offset, $whence = SEEK_SET) {
  48. return false;
  49. }
  50. }
  51. /**
  52. * @group PRIMARY-s3
  53. */
  54. class S3Test extends ObjectStoreTest {
  55. public function setUp(): void {
  56. parent::setUp();
  57. $s3 = $this->getInstance();
  58. $s3->deleteObject('multiparttest');
  59. }
  60. protected function getInstance() {
  61. $config = \OC::$server->getConfig()->getSystemValue('objectstore');
  62. if (!is_array($config) || $config['class'] !== S3::class) {
  63. $this->markTestSkipped('objectstore not configured for s3');
  64. }
  65. return new S3($config['arguments']);
  66. }
  67. public function testUploadNonSeekable() {
  68. $this->cleanupAfter('multiparttest');
  69. $s3 = $this->getInstance();
  70. $s3->writeObject('multiparttest', NonSeekableStream::wrap(fopen(__FILE__, 'r')));
  71. $result = $s3->readObject('multiparttest');
  72. $this->assertEquals(file_get_contents(__FILE__), stream_get_contents($result));
  73. }
  74. public function testSeek() {
  75. $this->cleanupAfter('seek');
  76. $data = file_get_contents(__FILE__);
  77. $instance = $this->getInstance();
  78. $instance->writeObject('seek', $this->stringToStream($data));
  79. $read = $instance->readObject('seek');
  80. $this->assertEquals(substr($data, 0, 100), fread($read, 100));
  81. fseek($read, 10);
  82. $this->assertEquals(substr($data, 10, 100), fread($read, 100));
  83. fseek($read, 100, SEEK_CUR);
  84. $this->assertEquals(substr($data, 210, 100), fread($read, 100));
  85. }
  86. public function assertNoUpload($objectUrn) {
  87. $s3 = $this->getInstance();
  88. $s3client = $s3->getConnection();
  89. $uploads = $s3client->listMultipartUploads([
  90. 'Bucket' => $s3->getBucket(),
  91. 'Prefix' => $objectUrn,
  92. ]);
  93. $this->assertArrayNotHasKey('Uploads', $uploads);
  94. }
  95. public function testEmptyUpload() {
  96. $s3 = $this->getInstance();
  97. $emptyStream = fopen("php://memory", "r");
  98. fwrite($emptyStream, null);
  99. $s3->writeObject('emptystream', $emptyStream);
  100. $this->assertNoUpload('emptystream');
  101. $this->assertTrue($s3->objectExists('emptystream'));
  102. $thrown = false;
  103. try {
  104. self::assertFalse($s3->readObject('emptystream'));
  105. } catch (\Exception $e) {
  106. // An exception is expected here since 0 byte files are wrapped
  107. // to be read from an empty memory stream in the ObjectStoreStorage
  108. $thrown = true;
  109. }
  110. self::assertTrue($thrown, 'readObject with range requests are not expected to work on empty objects');
  111. $s3->deleteObject('emptystream');
  112. }
  113. /** File size to upload in bytes */
  114. public function dataFileSizes() {
  115. return [
  116. [1000000], [2000000], [5242879], [5242880], [5242881], [10000000]
  117. ];
  118. }
  119. /** @dataProvider dataFileSizes */
  120. public function testFileSizes($size) {
  121. $this->cleanupAfter('testfilesizes');
  122. $s3 = $this->getInstance();
  123. $sourceStream = fopen('php://memory', 'wb+');
  124. $writeChunkSize = 1024;
  125. $chunkCount = $size / $writeChunkSize;
  126. for ($i = 0; $i < $chunkCount; $i++) {
  127. fwrite($sourceStream, str_repeat('A',
  128. ($i < $chunkCount - 1) ? $writeChunkSize : $size - ($i * $writeChunkSize)
  129. ));
  130. }
  131. rewind($sourceStream);
  132. $s3->writeObject('testfilesizes', $sourceStream);
  133. $this->assertNoUpload('testfilesizes');
  134. self::assertTrue($s3->objectExists('testfilesizes'));
  135. $result = $s3->readObject('testfilesizes');
  136. // compare first 100 bytes
  137. self::assertEquals(str_repeat('A', 100), fread($result, 100));
  138. // compare 100 bytes
  139. fseek($result, $size - 100);
  140. self::assertEquals(str_repeat('A', 100), fread($result, 100));
  141. // end of file reached
  142. fseek($result, $size);
  143. self::assertTrue(feof($result));
  144. $this->assertNoUpload('testfilesizes');
  145. }
  146. }