1
0

S3Test.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace Test\Files\ObjectStore;
  7. use Icewind\Streams\Wrapper;
  8. use OC\Files\ObjectStore\S3;
  9. class MultiPartUploadS3 extends S3 {
  10. public function writeObject($urn, $stream, ?string $mimetype = null) {
  11. $this->getConnection()->upload($this->bucket, $urn, $stream, 'private', [
  12. 'mup_threshold' => 1,
  13. ]);
  14. }
  15. }
  16. class NonSeekableStream extends Wrapper {
  17. public static function wrap($source) {
  18. $context = stream_context_create([
  19. 'nonseek' => [
  20. 'source' => $source,
  21. ],
  22. ]);
  23. return Wrapper::wrapSource($source, $context, 'nonseek', self::class);
  24. }
  25. public function dir_opendir($path, $options) {
  26. return false;
  27. }
  28. public function stream_open($path, $mode, $options, &$opened_path) {
  29. $this->loadContext('nonseek');
  30. return true;
  31. }
  32. public function stream_seek($offset, $whence = SEEK_SET) {
  33. return false;
  34. }
  35. }
  36. /**
  37. * @group PRIMARY-s3
  38. */
  39. class S3Test extends ObjectStoreTest {
  40. public function setUp(): void {
  41. parent::setUp();
  42. $s3 = $this->getInstance();
  43. $s3->deleteObject('multiparttest');
  44. }
  45. protected function getInstance() {
  46. $config = \OC::$server->getConfig()->getSystemValue('objectstore');
  47. if (!is_array($config) || $config['class'] !== S3::class) {
  48. $this->markTestSkipped('objectstore not configured for s3');
  49. }
  50. return new S3($config['arguments']);
  51. }
  52. public function testUploadNonSeekable() {
  53. $this->cleanupAfter('multiparttest');
  54. $s3 = $this->getInstance();
  55. $s3->writeObject('multiparttest', NonSeekableStream::wrap(fopen(__FILE__, 'r')));
  56. $result = $s3->readObject('multiparttest');
  57. $this->assertEquals(file_get_contents(__FILE__), stream_get_contents($result));
  58. }
  59. public function testSeek() {
  60. $this->cleanupAfter('seek');
  61. $data = file_get_contents(__FILE__);
  62. $instance = $this->getInstance();
  63. $instance->writeObject('seek', $this->stringToStream($data));
  64. $read = $instance->readObject('seek');
  65. $this->assertEquals(substr($data, 0, 100), fread($read, 100));
  66. fseek($read, 10);
  67. $this->assertEquals(substr($data, 10, 100), fread($read, 100));
  68. fseek($read, 100, SEEK_CUR);
  69. $this->assertEquals(substr($data, 210, 100), fread($read, 100));
  70. }
  71. public function assertNoUpload($objectUrn) {
  72. /** @var \OC\Files\ObjectStore\S3 */
  73. $s3 = $this->getInstance();
  74. $s3client = $s3->getConnection();
  75. $uploads = $s3client->listMultipartUploads([
  76. 'Bucket' => $s3->getBucket(),
  77. 'Prefix' => $objectUrn,
  78. ]);
  79. $this->assertArrayNotHasKey('Uploads', $uploads, 'Assert is not uploaded');
  80. }
  81. public function testEmptyUpload() {
  82. $s3 = $this->getInstance();
  83. $emptyStream = fopen("php://memory", "r");
  84. fwrite($emptyStream, '');
  85. $s3->writeObject('emptystream', $emptyStream);
  86. $this->assertNoUpload('emptystream');
  87. $this->assertTrue($s3->objectExists('emptystream'), 'Object exists on S3');
  88. $thrown = false;
  89. try {
  90. self::assertFalse($s3->readObject('emptystream'), 'Reading empty stream object should return false');
  91. } catch (\Exception $e) {
  92. // An exception is expected here since 0 byte files are wrapped
  93. // to be read from an empty memory stream in the ObjectStoreStorage
  94. $thrown = true;
  95. }
  96. self::assertTrue($thrown, 'readObject with range requests are not expected to work on empty objects');
  97. $s3->deleteObject('emptystream');
  98. }
  99. /** File size to upload in bytes */
  100. public function dataFileSizes() {
  101. return [
  102. [1000000], [2000000], [5242879], [5242880], [5242881], [10000000]
  103. ];
  104. }
  105. /** @dataProvider dataFileSizes */
  106. public function testFileSizes($size) {
  107. $this->cleanupAfter('testfilesizes');
  108. $s3 = $this->getInstance();
  109. $sourceStream = fopen('php://memory', 'wb+');
  110. $writeChunkSize = 1024;
  111. $chunkCount = $size / $writeChunkSize;
  112. for ($i = 0; $i < $chunkCount; $i++) {
  113. fwrite($sourceStream, str_repeat('A',
  114. ($i < $chunkCount - 1) ? $writeChunkSize : $size - ($i * $writeChunkSize)
  115. ));
  116. }
  117. rewind($sourceStream);
  118. $s3->writeObject('testfilesizes', $sourceStream);
  119. $this->assertNoUpload('testfilesizes');
  120. self::assertTrue($s3->objectExists('testfilesizes'), 'Object exists on S3');
  121. $result = $s3->readObject('testfilesizes');
  122. // compare first 100 bytes
  123. self::assertEquals(str_repeat('A', 100), fread($result, 100), 'Compare first 100 bytes');
  124. // compare last 100 bytes
  125. fseek($result, $size - 100);
  126. self::assertEquals(str_repeat('A', 100), fread($result, 100), 'Compare last 100 bytes');
  127. // end of file reached
  128. fseek($result, $size);
  129. self::assertTrue(feof($result), 'End of file reached');
  130. $this->assertNoUpload('testfilesizes');
  131. }
  132. }