object.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. const debug = require('debug')('ali-oss:object');
  2. const fs = require('fs');
  3. const is = require('is-type-of');
  4. const copy = require('copy-to');
  5. const path = require('path');
  6. const mime = require('mime');
  7. const callback = require('./common/callback');
  8. const { Transform } = require('stream');
  9. const pump = require('pump');
  10. const { isBuffer } = require('./common/utils/isBuffer');
  11. const proto = exports;
  12. /**
  13. * Object operations
  14. */
  15. /**
  16. * append an object from String(file path)/Buffer/ReadableStream
  17. * @param {String} name the object key
  18. * @param {Mixed} file String(file path)/Buffer/ReadableStream
  19. * @param {Object} options
  20. * @return {Object}
  21. */
  22. proto.append = async function append(name, file, options) {
  23. options = options || {};
  24. if (options.position === undefined) options.position = '0';
  25. options.subres = {
  26. append: '',
  27. position: options.position
  28. };
  29. options.method = 'POST';
  30. const result = await this.put(name, file, options);
  31. result.nextAppendPosition = result.res.headers['x-oss-next-append-position'];
  32. return result;
  33. };
  34. /**
  35. * put an object from String(file path)/Buffer/ReadableStream
  36. * @param {String} name the object key
  37. * @param {Mixed} file String(file path)/Buffer/ReadableStream
  38. * @param {Object} options
  39. * {Object} options.callback The callback parameter is composed of a JSON string encoded in Base64
  40. * {String} options.callback.url the OSS sends a callback request to this URL
  41. * {String} options.callback.host The host header value for initiating callback requests
  42. * {String} options.callback.body The value of the request body when a callback is initiated
  43. * {String} options.callback.contentType The Content-Type of the callback requests initiatiated
  44. * {Object} options.callback.customValue Custom parameters are a map of key-values, e.g:
  45. * customValue = {
  46. * key1: 'value1',
  47. * key2: 'value2'
  48. * }
  49. * @return {Object}
  50. */
  51. proto.put = async function put(name, file, options) {
  52. let content;
  53. options = options || {};
  54. name = this._objectName(name);
  55. if (isBuffer(file)) {
  56. content = file;
  57. } else if (is.string(file)) {
  58. const stats = fs.statSync(file);
  59. if (!stats.isFile()) {
  60. throw new Error(`${file} is not file`);
  61. }
  62. options.mime = options.mime || mime.getType(path.extname(file));
  63. const stream = fs.createReadStream(file);
  64. options.contentLength = await this._getFileSize(file);
  65. return await this.putStream(name, stream, options);
  66. } else if (is.readableStream(file)) {
  67. return await this.putStream(name, file, options);
  68. } else {
  69. throw new TypeError('Must provide String/Buffer/ReadableStream for put.');
  70. }
  71. options.headers = options.headers || {};
  72. this._convertMetaToHeaders(options.meta, options.headers);
  73. const method = options.method || 'PUT';
  74. const params = this._objectRequestParams(method, name, options);
  75. callback.encodeCallback(params, options);
  76. params.mime = options.mime;
  77. params.content = content;
  78. params.successStatuses = [200];
  79. const result = await this.request(params);
  80. const ret = {
  81. name,
  82. url: this._objectUrl(name),
  83. res: result.res
  84. };
  85. if (params.headers && params.headers['x-oss-callback']) {
  86. ret.data = JSON.parse(result.data.toString());
  87. }
  88. return ret;
  89. };
  90. /**
  91. * put an object from ReadableStream. If `options.contentLength` is
  92. * not provided, chunked encoding is used.
  93. * @param {String} name the object key
  94. * @param {Readable} stream the ReadableStream
  95. * @param {Object} options
  96. * @return {Object}
  97. */
  98. proto.putStream = async function putStream(name, stream, options) {
  99. options = options || {};
  100. options.headers = options.headers || {};
  101. name = this._objectName(name);
  102. if (options.contentLength) {
  103. options.headers['Content-Length'] = options.contentLength;
  104. } else {
  105. options.headers['Transfer-Encoding'] = 'chunked';
  106. }
  107. this._convertMetaToHeaders(options.meta, options.headers);
  108. const method = options.method || 'PUT';
  109. const params = this._objectRequestParams(method, name, options);
  110. callback.encodeCallback(params, options);
  111. params.mime = options.mime;
  112. const transform = new Transform();
  113. // must remove http stream header for signature
  114. transform._transform = function _transform(chunk, encoding, done) {
  115. this.push(chunk);
  116. done();
  117. };
  118. params.stream = pump(stream, transform);
  119. params.successStatuses = [200];
  120. const result = await this.request(params);
  121. const ret = {
  122. name,
  123. url: this._objectUrl(name),
  124. res: result.res
  125. };
  126. if (params.headers && params.headers['x-oss-callback']) {
  127. ret.data = JSON.parse(result.data.toString());
  128. }
  129. return ret;
  130. };
  131. proto.getStream = async function getStream(name, options) {
  132. options = options || {};
  133. if (options.process) {
  134. options.subres = options.subres || {};
  135. options.subres['x-oss-process'] = options.process;
  136. }
  137. const params = this._objectRequestParams('GET', name, options);
  138. params.customResponse = true;
  139. params.successStatuses = [200, 206, 304];
  140. const result = await this.request(params);
  141. return {
  142. stream: result.res,
  143. res: {
  144. status: result.status,
  145. headers: result.headers
  146. }
  147. };
  148. };
  149. proto.putMeta = async function putMeta(name, meta, options) {
  150. return await this.copy(name, name, {
  151. meta: meta || {},
  152. timeout: options && options.timeout,
  153. ctx: options && options.ctx
  154. });
  155. };
  156. proto.list = async function list(query, options) {
  157. // prefix, marker, max-keys, delimiter
  158. const params = this._objectRequestParams('GET', '', options);
  159. params.query = query;
  160. params.xmlResponse = true;
  161. params.successStatuses = [200];
  162. const result = await this.request(params);
  163. let objects = result.data.Contents;
  164. const that = this;
  165. if (objects) {
  166. if (!Array.isArray(objects)) {
  167. objects = [objects];
  168. }
  169. objects = objects.map(obj => ({
  170. name: obj.Key,
  171. url: that._objectUrl(obj.Key),
  172. lastModified: obj.LastModified,
  173. etag: obj.ETag,
  174. type: obj.Type,
  175. size: Number(obj.Size),
  176. storageClass: obj.StorageClass,
  177. owner: {
  178. id: obj.Owner.ID,
  179. displayName: obj.Owner.DisplayName
  180. }
  181. }));
  182. }
  183. let prefixes = result.data.CommonPrefixes || null;
  184. if (prefixes) {
  185. if (!Array.isArray(prefixes)) {
  186. prefixes = [prefixes];
  187. }
  188. prefixes = prefixes.map(item => item.Prefix);
  189. }
  190. return {
  191. res: result.res,
  192. objects,
  193. prefixes,
  194. nextMarker: result.data.NextMarker || null,
  195. isTruncated: result.data.IsTruncated === 'true'
  196. };
  197. };
  198. proto.listV2 = async function listV2(query, options) {
  199. const params = this._objectRequestParams('GET', '', options);
  200. params.query = {
  201. 'list-type': 2,
  202. ...query
  203. };
  204. params.xmlResponse = true;
  205. params.successStatuses = [200];
  206. const result = await this.request(params);
  207. let objects = result.data.Contents;
  208. const that = this;
  209. if (objects) {
  210. if (!Array.isArray(objects)) {
  211. objects = [objects];
  212. }
  213. objects = objects.map(obj => ({
  214. name: obj.Key,
  215. url: that._objectUrl(obj.Key),
  216. lastModified: obj.LastModified,
  217. etag: obj.ETag,
  218. type: obj.Type,
  219. size: Number(obj.Size),
  220. storageClass: obj.StorageClass,
  221. owner: obj.Owner
  222. ? {
  223. id: obj.Owner.ID,
  224. displayName: obj.Owner.DisplayName
  225. }
  226. : null
  227. }));
  228. }
  229. let prefixes = result.data.CommonPrefixes || null;
  230. if (prefixes) {
  231. if (!Array.isArray(prefixes)) {
  232. prefixes = [prefixes];
  233. }
  234. prefixes = prefixes.map(item => item.Prefix);
  235. }
  236. return {
  237. res: result.res,
  238. objects,
  239. prefixes,
  240. isTruncated: result.data.IsTruncated === 'true',
  241. keyCount: result.data.KeyCount,
  242. continuationToken: result.data.ContinuationToken || null,
  243. nextContinuationToken: result.data.NextContinuationToken || null
  244. };
  245. };
  246. /**
  247. * Restore Object
  248. * @param {String} name the object key
  249. * @param {Object} options
  250. * @returns {{res}}
  251. */
  252. proto.restore = async function restore(name, options) {
  253. options = options || {};
  254. options.subres = Object.assign({ restore: '' }, options.subres);
  255. if (options.versionId) {
  256. options.subres.versionId = options.versionId;
  257. }
  258. const params = this._objectRequestParams('POST', name, options);
  259. params.successStatuses = [202];
  260. const result = await this.request(params);
  261. return {
  262. res: result.res
  263. };
  264. };
  265. proto._objectUrl = function _objectUrl(name) {
  266. return this._getReqUrl({ bucket: this.options.bucket, object: name });
  267. };
  268. /**
  269. * generator request params
  270. * @return {Object} params
  271. *
  272. * @api private
  273. */
  274. proto._objectRequestParams = function (method, name, options) {
  275. if (!this.options.bucket) {
  276. throw new Error('Please create a bucket first');
  277. }
  278. options = options || {};
  279. name = this._objectName(name);
  280. const params = {
  281. object: name,
  282. bucket: this.options.bucket,
  283. method,
  284. subres: options && options.subres,
  285. timeout: options && options.timeout,
  286. ctx: options && options.ctx
  287. };
  288. if (options.headers) {
  289. params.headers = {};
  290. copy(options.headers).to(params.headers);
  291. }
  292. return params;
  293. };
  294. proto._objectName = function (name) {
  295. return name.replace(/^\/+/, '');
  296. };
  297. proto._statFile = function (filepath) {
  298. return new Promise((resolve, reject) => {
  299. fs.stat(filepath, (err, stats) => {
  300. if (err) {
  301. reject(err);
  302. } else {
  303. resolve(stats);
  304. }
  305. });
  306. });
  307. };
  308. proto._convertMetaToHeaders = function (meta, headers) {
  309. if (!meta) {
  310. return;
  311. }
  312. Object.keys(meta).forEach(k => {
  313. headers[`x-oss-meta-${k}`] = meta[k];
  314. });
  315. };
  316. proto._deleteFileSafe = function (filepath) {
  317. return new Promise(resolve => {
  318. fs.exists(filepath, exists => {
  319. if (!exists) {
  320. resolve();
  321. } else {
  322. fs.unlink(filepath, err => {
  323. if (err) {
  324. debug('unlink %j error: %s', filepath, err);
  325. }
  326. resolve();
  327. });
  328. }
  329. });
  330. });
  331. };